C Programming Interview Questions and Answers
C questions that campus tests and embedded-company interviews ask again and again, with clear answers. Includes predict-the-output questions on pointers, strings and recursion.
Q1. What does the & operator give when used as &x?
- A. A copy of x
- B. The value of x
- C. x multiplied by itself
- D. The address of x
Show answerHide answer
Answer: D. The address of x
&x is the memory address of x, which is why scanf needs &x to write into it.Q2. Which header file declares printf() and scanf()?
- A. conio.h
- B. stdlib.h
- C. string.h
- D. stdio.h
Show answerHide answer
Answer: D. stdio.h
stdio.h is the standard input/output header.Q3. What character ends every C string?
- A. '\n'
- B. '\0' (the null character)
- C. A space
- D. '$'
Show answerHide answer
Answer: B. '\0' (the null character)
C strings are char arrays terminated by '\0'; strlen counts up to it.Q4. Which function allocates memory on the heap?
- A. malloc()
- B. alloc()
- C. new()
- D. sizeof()
Show answerHide answer
Answer: A. malloc()
malloc returns a pointer to a heap block (or NULL); release it with free().Q5. What happens if you forget to free() memory you malloc'd?
- A. Nothing ever
- B. A memory leak
- C. The program crashes immediately
- D. A compile error
Show answerHide answer
Answer: B. A memory leak
The memory stays reserved until the program ends.Q6. Which format specifier prints a double with printf?
- A. %f
- B. %d
- C. %c
- D. %s
Show answerHide answer
Answer: A. %f
%f (or %lf) prints floating-point values; scanf needs %lf for a double.Q7. What is a pointer?
- A. A variable that stores a memory address
- B. A function that points to code
- C. A keyword for loops
- D. A special type of array
Show answerHide answer
Answer: A. A variable that stores a memory address
int *p = &x; stores the address of x in p.Q8. What is the difference between a struct and a union?
- A. A union's members share the same memory
- B. A struct can't hold arrays
- C. A union can't hold ints
- D. There is no difference
Show answerHide answer
Answer: A. A union's members share the same memory
A struct gives each member its own space; a union overlaps them, so its size is that of the largest member.Q9. Which loop always runs its body at least once?
- A. do-while
- B. for
- C. while
- D. None of them
Show answerHide answer
Answer: A. do-while
do { } while (cond); checks the condition after the body.Q10. What is the value of an uninitialised local int in C?
- A. NULL
- B. Unpredictable (garbage)
- C. 0
- D. -1
Show answerHide answer
Answer: B. Unpredictable (garbage)
Local variables are not initialised automatically; globals and statics start at 0.Q11. Which keyword keeps a local variable's value between function calls?
- A. extern
- B. register
- C. static
- D. const
Show answerHide answer
Answer: C. static
A static local is created once and keeps its value across calls.Q12. What does strcmp(a, b) return when the strings are equal?
- A. -1
- B. 1
- C. 0
- D. true
Show answerHide answer
Answer: C. 0
strcmp returns 0 for equal strings, negative if a < b and positive if a > b.Q13. Why can't you compare two strings with == in C?
- A. It only compares the first character
- B. It compares addresses, not contents
- C. It always returns true
- D. It is a syntax error
Show answerHide answer
Answer: B. It compares addresses, not contents
Array names decay to pointers; use strcmp to compare the text.Q14. What is a dangling pointer?
- A. A pointer to a function
- B. A pointer to memory that has already been freed
- C. A pointer to a pointer
- D. A pointer that is NULL
Show answerHide answer
Answer: B. A pointer to memory that has already been freed
Using it is undefined behaviour; set pointers to NULL after free().Q15. How are arrays passed to functions in C?
- A. As a full copy
- B. They can't be passed
- C. By value of the last element
- D. As a pointer to the first element
Show answerHide answer
Answer: D. As a pointer to the first element
So the function can change the caller's array, and sizeof inside the function gives the pointer size.Q16. What does the preprocessor directive #define PI 3.14 do?
- A. Creates a variable named PI
- B. Imports a maths library
- C. Declares a function PI
- D. Replaces PI with 3.14 before compilation
Show answerHide answer
Answer: D. Replaces PI with 3.14 before compilation
Macros are text substitutions done by the preprocessor.Q17. Which operator accesses a struct member through a pointer?
- A. ::
- B. ->
- C. .
- D. &
Show answerHide answer
Answer: B. ->
p->x is shorthand for (*p).x.Q18. What is the size of a char in C?
- A. 1 byte
- B. 2 bytes
- C. 4 bytes
- D. Depends on the string
Show answerHide answer
Answer: A. 1 byte
sizeof(char) is always 1 by definition.Q19. What does recursion need so it doesn't run forever?
- A. A global variable
- B. A pointer
- C. A while loop
- D. A base case
Show answerHide answer
Answer: D. A base case
Without a base case the calls never stop and the stack overflows.Q20. What does calloc() do differently from malloc()?
- A. It sets the allocated memory to zero
- B. It allocates on the stack
- C. It frees memory
- D. It resizes memory
Show answerHide answer
Answer: A. It sets the allocated memory to zero
calloc(n, size) allocates n * size bytes and zeroes them.Q21. What does this code print?
int a = 5, b = 2; printf("%d\n", a / b);- A. 3
- B. 2
- C. 2.5
- D. 2.000000
Show answerHide answer
Answer: B. 2
Integer division of two ints drops the fraction.Q22. What does this code print?
int a = 5, b = 2; printf("%.2f\n", (double) a / b);- A. 2.00
- B. 2.5
- C. 2.50
- D. 2
Show answerHide answer
Answer: C. 2.50
Casting a to double makes the division happen in floating point.Q23. What does this code print?
int x = 5; int y = ++x; printf("%d %d\n", y, x);- A. 6 5
- B. 5 6
- C. 5 5
- D. 6 6
Show answerHide answer
Answer: D. 6 6
++x increments first, so y gets 6 and x is 6.Q24. What does this code print?
#include <stdio.h> void set(int *p) { *p = 20; } int main() { int x = 10; set(&x); printf("%d\n", x); return 0; }- A. 10
- B. 0
- C. 20
- D. Error
Show answerHide answer
Answer: C. 20
The function receives the address of x and changes x through the pointer.Q25. What does this code print?
#include <stdio.h> void set(int v) { v = 20; } int main() { int x = 10; set(x); printf("%d\n", x); return 0; }- A. Error
- B. 20
- C. 0
- D. 10
Show answerHide answer
Answer: D. 10
C passes a copy of the value; the function changes only its copy.Q26. What does this code print?
char s[] = "hello"; printf("%zu\n", strlen(s));- A. Error
- B. 5
- C. 6
- D. 4
Show answerHide answer
Answer: B. 5
strlen counts the characters before '\0': h, e, l, l, o.Q27. What does this code print?
char s[] = "hello"; printf("%zu\n", sizeof(s));- A. 5
- B. 6
- C. 1
- D. 8
Show answerHide answer
Answer: B. 6
sizeof includes the terminating '\0': 5 letters + 1.Q28. What does this code print?
int arr[] = {10, 20, 30, 40}; int *p = arr; printf("%d\n", *(p + 2));- A. 10
- B. 30
- C. 20
- D. 40
Show answerHide answer
Answer: B. 30
p points to arr[0]; p + 2 points to arr[2].Q29. What does this code print?
int n = 2; switch (n) { case 1: printf("One "); case 2: printf("Two "); case 3: printf("Three\n"); break; default: printf("Default\n"); }- A. Three
- B. Two
- C. Two Three
- D. One Two Three
Show answerHide answer
Answer: C. Two Three
Without break, case 2 falls through into case 3.Q30. What does this code print?
#include <stdio.h> int next(void) { static int count = 0; return ++count; } int main() { int a = next(), b = next(), c = next(); printf("%d %d %d\n", a, b, c); return 0; }- A. 1 1 1
- B. 3 3 3
- C. 1 2 3
- D. 0 1 2
Show answerHide answer
Answer: C. 1 2 3
A static local keeps its value between calls.Q31. What does this code print?
for (int i = 0; i < 3; i++) printf(i < 2 ? "%d " : "%d\n", i);
- A. 0 1 2 3
- B. 0 1 2
- C. 1 2 3
- D. Infinite loop
Show answerHide answer
Answer: B. 0 1 2
The loop runs while i < 3.Q32. What does this code print?
char ch = 'A'; printf("%c\n", ch + 1);- A. A
- B. 66
- C. C
- D. B
Show answerHide answer
Answer: D. B
'A' + 1 is 66, which %c prints as 'B'.Q33. What does this code print?
printf("%d\n", 5 > 3);- A. 0
- B. 5
- C. 1
- D. Error
Show answerHide answer
Answer: C. 1
In C, the comparison 5 > 3 evaluates to the int 1 (true).Q34. What does this code print?
char a[] = "cat", b[] = "cat"; printf(strcmp(a, b) == 0 ? "Equal\n" : "Not equal\n");
- A. Equal
- B. Not equal
- C. Error
- D. 0
Show answerHide answer
Answer: A. Equal
strcmp returns 0 when the two strings have the same characters.Q35. What does this code print?
int x = 10 % 7; printf("%d\n", x);- A. 4
- B. 3
- C. 1
- D. 2
Show answerHide answer
Answer: B. 3
x = 10 % 7 = 3.Q36. What does this code print?
#include <stdio.h> void swap(int *a, int *b) { int t = *a; *a = *b; *b = t; } int main() { int x = 1, y = 2; swap(&x, &y); printf("%d %d\n", x, y); return 0; }- A. 1 1
- B. 2 2
- C. 1 2
- D. 2 1
Show answerHide answer
Answer: D. 2 1
swap receives addresses and exchanges the values through the pointers.Q37. What does this code print?
#include <stdio.h> int fact(int n) { return n <= 1 ? 1 : n * fact(n - 1); } int main() { printf("%d\n", fact(4)); return 0; }- A. 10
- B. 4
- C. 24
- D. 120
Show answerHide answer
Answer: C. 24
fact(4) = 4 × 3 × 2 × 1 = 24.Q38. What does this code print?
#include <stdio.h> struct Student { char name[20]; int marks; }; int main() { struct Student s = {"Kavin", 90}; printf("%s %d\n", s.name, s.marks); return 0; }- A. 90 Kavin
- B. Kavin 90
- C. Kavin 0
- D. Error
Show answerHide answer
Answer: B. Kavin 90
Members of a struct are accessed with the dot operator.
Practised these? Now prove it.
Take a timed mock test with new questions every attempt, earn a verified certificate at 70%+, and get noticed by employers.
Start a mock test