C Programming interview questions

19 questions from the C Programming Fundamentals: Pointers, Memory and Files course, each with the correct answer and an explanation. Try to answer before you open the answer.

Module 1: Getting Started

1. What does `\n` do in `printf`?

  1. Prints a backslash and n
  2. Ends the program
  3. Moves output to a new line
  4. Ends the program
Show answer

Answer: Moves output to a new line

`\n` is the newline escape sequence.

Lesson: Hello, World!

Module 2: Basics

2. In C, what is `7 / 2`?

  1. 3.5
  2. 4
  3. 3.0
  4. 3
Show answer

Answer: 3

Both operands are int, so the result is integer-divided to 3.

Lesson: Operators

Module 3: Control Flow

3. Which loop always runs its body at least once?

  1. do ... while
  2. while
  3. for
  4. switch
Show answer

Answer: do ... while

do ... while checks its condition after the body.

Lesson: Loops

Module 4: Functions

4. What happens to a normal (non-static) local variable when its function returns?

  1. It keeps its value for next call
  2. It is destroyed
  3. It becomes global
  4. It is copied to the next call
Show answer

Answer: It is destroyed

Local variables live on the stack and are freed once the function returns.

Lesson: Scope & Storage

5. What does a function pointer store?

  1. The address of a function
  2. The return value of a function
  3. A copy of the function body
  4. The number of arguments
Show answer

Answer: The address of a function

You call the function through that address.

Lesson: Function Pointers & Callbacks

Module 5: Arrays & Strings

6. Why should you avoid `if (str1 == str2)` to compare two C strings?

  1. It always returns true
  2. It only works with numbers
  3. It compares pointer addresses, not contents
  4. It is slower than strcmp
Show answer

Answer: It compares pointer addresses, not contents

Arrays decay to pointers, so `==` compares addresses; use `strcmp` for content.

Lesson: string.h Functions

Module 6: Pointers

7. Why does `swap(int x, int y)` (no pointers) fail to swap the caller's variables?

  1. C doesn't allow swapping
  2. The compiler optimizes it away
  3. Pointers cannot be passed
  4. x and y are copies of the originals
Show answer

Answer: x and y are copies of the originals

Without pointers, the function only swaps its own local copies.

Lesson: Pointers & Functions

Module 7: Structures & Unions

8. What does `s->field` mean when `s` is a pointer to a struct?

  1. Shorthand for (*s).field
  2. It compares s and field
  3. It creates a new struct
  4. Declares a pointer
Show answer

Answer: Shorthand for (*s).field

`->` dereferences the pointer and accesses the member in one step.

Lesson: Arrays of Structures

Module 8: Dynamic Memory

9. What should you do to a pointer immediately after calling free() on it?

  1. Call free() on it again
  2. Set it to NULL
  3. Nothing, it's fine to keep using it
  4. Cast it to void
Show answer

Answer: Set it to NULL

Setting it to NULL prevents accidental use-after-free bugs.

Lesson: Memory Leaks & Dangling Pointers

Module 9: File Handling

10. What does fopen return if the file cannot be opened?

  1. An empty string
  2. It crashes the program
  3. NULL
  4. It returns 0
Show answer

Answer: NULL

fopen returns NULL on failure -- always check before using the handle.

Lesson: Reading & Writing Files

Module 11: Bit Manipulation

11. Which operator flips every bit of a value?

  1. ^ (XOR)
  2. & (AND)
  3. << (left shift)
  4. ~ (NOT)
Show answer

Answer: ~ (NOT)

The bitwise NOT (~) inverts every bit of its operand.

Lesson: Bitwise Operators & Bit Flags

Module 12: Common Pitfalls & Best Practices

12. What does gcc hello.c -o hello do?

  1. Runs the program
  2. Deletes hello.c
  3. Compiles the source into an executable
  4. Installs gcc
Show answer

Answer: Compiles the source into an executable

-o names the output file.

Lesson: Debugging Basics

13. What is 7 / 2 in C with ints?

  1. 3.5
  2. 4
  3. 2
  4. 3
Show answer

Answer: 3

Integer division truncates.

Lesson: Debugging Basics

14. Why use fgets instead of scanf("%s")?

  1. It limits input to the buffer size
  2. It is shorter
  3. It parses numbers
  4. It ignores spaces
Show answer

Answer: It limits input to the buffer size

scanf %s has no bounds checking.

Lesson: Debugging Basics

15. What does *p do when p is a pointer?

  1. Gets its address
  2. Accesses the value it points to
  3. Frees it
  4. Declares it
Show answer

Answer: Accesses the value it points to

* dereferences a pointer.

Lesson: Debugging Basics

16. Why does swap(int x, int y) fail to swap the caller's variables?

  1. C forbids swapping
  2. The compiler removes it
  3. Arguments are copied
  4. It needs a header
Show answer

Answer: Arguments are copied

Pass pointers to modify the originals.

Lesson: Debugging Basics

17. Which function allocates memory on the heap?

  1. printf
  2. strlen
  3. fopen
  4. malloc
Show answer

Answer: malloc

Pair every malloc with one free.

Lesson: Debugging Basics

18. What should you do with a pointer right after free()?

  1. Set it to NULL
  2. Free it again
  3. Use it
  4. Cast it to int
Show answer

Answer: Set it to NULL

It avoids dangling-pointer use.

Lesson: Debugging Basics

19. Which tool can detect buffer overflows at runtime?

  1. make clean
  2. AddressSanitizer
  3. ls
  4. diff
Show answer

Answer: AddressSanitizer

Compile with -fsanitize=address.

Lesson: Debugging Basics

← Back to the course