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.

  1. 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 answer

    Answer: D. The address of x
    &x is the memory address of x, which is why scanf needs &x to write into it.

  2. Q2. Which header file declares printf() and scanf()?

    • A. conio.h
    • B. stdlib.h
    • C. string.h
    • D. stdio.h
    Show answer

    Answer: D. stdio.h
    stdio.h is the standard input/output header.

  3. Q3. What character ends every C string?

    • A. '\n'
    • B. '\0' (the null character)
    • C. A space
    • D. '$'
    Show answer

    Answer: B. '\0' (the null character)
    C strings are char arrays terminated by '\0'; strlen counts up to it.

  4. Q4. Which function allocates memory on the heap?

    • A. malloc()
    • B. alloc()
    • C. new()
    • D. sizeof()
    Show answer

    Answer: A. malloc()
    malloc returns a pointer to a heap block (or NULL); release it with free().

  5. 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 answer

    Answer: B. A memory leak
    The memory stays reserved until the program ends.

  6. Q6. Which format specifier prints a double with printf?

    • A. %f
    • B. %d
    • C. %c
    • D. %s
    Show answer

    Answer: A. %f
    %f (or %lf) prints floating-point values; scanf needs %lf for a double.

  7. 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 answer

    Answer: A. A variable that stores a memory address
    int *p = &x; stores the address of x in p.

  8. 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 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.

  9. Q9. Which loop always runs its body at least once?

    • A. do-while
    • B. for
    • C. while
    • D. None of them
    Show answer

    Answer: A. do-while
    do { } while (cond); checks the condition after the body.

  10. Q10. What is the value of an uninitialised local int in C?

    • A. NULL
    • B. Unpredictable (garbage)
    • C. 0
    • D. -1
    Show answer

    Answer: B. Unpredictable (garbage)
    Local variables are not initialised automatically; globals and statics start at 0.

  11. Q11. Which keyword keeps a local variable's value between function calls?

    • A. extern
    • B. register
    • C. static
    • D. const
    Show answer

    Answer: C. static
    A static local is created once and keeps its value across calls.

  12. Q12. What does strcmp(a, b) return when the strings are equal?

    • A. -1
    • B. 1
    • C. 0
    • D. true
    Show answer

    Answer: C. 0
    strcmp returns 0 for equal strings, negative if a < b and positive if a > b.

  13. 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 answer

    Answer: B. It compares addresses, not contents
    Array names decay to pointers; use strcmp to compare the text.

  14. 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 answer

    Answer: B. A pointer to memory that has already been freed
    Using it is undefined behaviour; set pointers to NULL after free().

  15. 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 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.

  16. 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 answer

    Answer: D. Replaces PI with 3.14 before compilation
    Macros are text substitutions done by the preprocessor.

  17. Q17. Which operator accesses a struct member through a pointer?

    • A. ::
    • B. ->
    • C. .
    • D. &
    Show answer

    Answer: B. ->
    p->x is shorthand for (*p).x.

  18. 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 answer

    Answer: A. 1 byte
    sizeof(char) is always 1 by definition.

  19. 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 answer

    Answer: D. A base case
    Without a base case the calls never stop and the stack overflows.

  20. 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 answer

    Answer: A. It sets the allocated memory to zero
    calloc(n, size) allocates n * size bytes and zeroes them.

  21. 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 answer

    Answer: B. 2
    Integer division of two ints drops the fraction.

  22. 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 answer

    Answer: C. 2.50
    Casting a to double makes the division happen in floating point.

  23. 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 answer

    Answer: D. 6 6
    ++x increments first, so y gets 6 and x is 6.

  24. 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 answer

    Answer: C. 20
    The function receives the address of x and changes x through the pointer.

  25. 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 answer

    Answer: D. 10
    C passes a copy of the value; the function changes only its copy.

  26. Q26. What does this code print?

    char s[] = "hello";
    printf("%zu\n", strlen(s));
    • A. Error
    • B. 5
    • C. 6
    • D. 4
    Show answer

    Answer: B. 5
    strlen counts the characters before '\0': h, e, l, l, o.

  27. Q27. What does this code print?

    char s[] = "hello";
    printf("%zu\n", sizeof(s));
    • A. 5
    • B. 6
    • C. 1
    • D. 8
    Show answer

    Answer: B. 6
    sizeof includes the terminating '\0': 5 letters + 1.

  28. 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 answer

    Answer: B. 30
    p points to arr[0]; p + 2 points to arr[2].

  29. 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 answer

    Answer: C. Two Three
    Without break, case 2 falls through into case 3.

  30. 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 answer

    Answer: C. 1 2 3
    A static local keeps its value between calls.

  31. 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 answer

    Answer: B. 0 1 2
    The loop runs while i < 3.

  32. Q32. What does this code print?

    char ch = 'A';
    printf("%c\n", ch + 1);
    • A. A
    • B. 66
    • C. C
    • D. B
    Show answer

    Answer: D. B
    'A' + 1 is 66, which %c prints as 'B'.

  33. Q33. What does this code print?

    printf("%d\n", 5 > 3);
    • A. 0
    • B. 5
    • C. 1
    • D. Error
    Show answer

    Answer: C. 1
    In C, the comparison 5 > 3 evaluates to the int 1 (true).

  34. 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 answer

    Answer: A. Equal
    strcmp returns 0 when the two strings have the same characters.

  35. Q35. What does this code print?

    int x = 10 % 7;
    printf("%d\n", x);
    • A. 4
    • B. 3
    • C. 1
    • D. 2
    Show answer

    Answer: B. 3
    x = 10 % 7 = 3.

  36. 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 answer

    Answer: D. 2 1
    swap receives addresses and exchanges the values through the pointers.

  37. 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 answer

    Answer: C. 24
    fact(4) = 4 × 3 × 2 × 1 = 24.

  38. 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 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