InterviewQs

Top 50+ C Interview Questions & Answers

Crack your C programming interview with these essential questions, answers, and coding challenges!

  1. What is C language?

    C is a general-purpose, procedural programming language developed by Dennis Ritchie at Bell Labs in the early 1970s. It's widely used for system programming, embedded systems, and developing operating systems due to its efficiency and low-level memory control.

  2. What are the main features of C language?

    Simple and efficient syntax; rich set of built-in operators; pointers; low-level memory access; structured programming; portability across platforms.

  3. Explain the difference between malloc() and calloc().

    malloc(size) allocates a block of memory without initializing it, while calloc(n, size) allocates and zero-initializes memory for an array of n elements.

  4. What is the difference between ++i and i++?

    ++i (pre-increment) increments i before evaluating the expression; i++ (post-increment) evaluates the expression first, then increments i.

  5. What is a pointer in C?

    A pointer is a variable that stores the memory address of another variable, enabling dynamic memory management, arrays, and function callbacks.

  6. Explain the concept of dynamic memory allocation in C.

    Dynamic memory allocation allows you to request and release memory at runtime using malloc(), calloc(), realloc(), and free().

  7. What is a memory leak and how can you prevent it?

    A memory leak happens when allocated memory isn’t freed. Prevent it by ensuring every malloc/calloc has a corresponding free().

  8. Describe the sizeof operator.

    sizeof returns the size in bytes of a data type or object on the target platform.

  9. How do you reverse a linked list in C?

    Iteratively reassign each node’s next pointer to its previous node, or use recursion to reverse sublists.

  10. Explain how to implement a stack using an array in C.

    Use an array and an integer top index: push() does array[++top] = value; pop() returns array[top--].

  11. What are function pointers and how are they used?

    Function pointers store addresses of functions and allow callbacks, dynamic dispatch, and function tables.

  12. Explain bitwise operators and their use cases.

    Operators like &, |, ^, ~, <<, >> operate on individual bits, useful for flags, masks, encryption, and performance-critical code.

  13. What are the differences between struct and union?

    struct allocates separate memory for each member; union shares one memory block for all members (size = largest member).

  14. What is the difference between struct and typedef struct?

    typedef struct creates an alias so you can use the type without the struct keyword when declaring variables.

  15. What is a segmentation fault?

    A segmentation fault occurs when a program accesses memory it’s not allowed to, usually due to invalid pointers.

  16. How does the assert macro work?

    assert(expr) checks expr and aborts the program with an error message if expr evaluates to false (debug builds).

  17. Write a C program to find the factorial of a number.

    Use either an iterative loop or a recursive function: fact(n) = n == 0 ? 1 : n * fact(n - 1).

  18. Implement a C program to reverse a string without built-ins.

    Swap characters from ends moving toward the center using two indices.

  19. How would you find the maximum element in an array?

    Iterate through the array, tracking the largest value seen so far.

  20. Write a C program to check if a number is a palindrome.

    Reverse the number by extracting digits and compare with the original.

  21. Write a C program to find the second largest element in an array.

    Maintain two variables for largest and second largest, update them in one pass.

  22. How do you remove duplicates from an array?

    Use nested loops or a hash table to track seen elements and shift unique elements forward.

  23. Implement a function to rotate an array right by k positions.

    Reverse the whole array, then reverse the first k elements and the remaining elements.

  24. Write a C program to check if two strings are anagrams.

    Count frequency of each character in both strings and compare the counts.

  25. Reverse the words in a given string.

    Split the string by spaces, reverse the word array, then rejoin with spaces.

  26. Detect a cycle in a linked list.

    Use Floyd’s Tortoise and Hare: move slow by 1 and fast by 2; if they meet, a cycle exists.

  27. Merge two sorted linked lists.

    Iteratively compare head nodes, attach the smaller to the merged list, advance pointer.

  28. Find the middle element of a linked list in one pass.

    Use slow and fast pointers—slow moves 1 step, fast moves 2; slow ends at middle.

  29. Count the number of 1s in the binary representation of an integer.

    Use n & (n-1) trick in a loop: count++ and n &= (n - 1) until n becomes 0.

  30. Swap two numbers without a temporary variable.

    Use XOR: a ^= b; b ^= a; a ^= b.

  31. Implement a queue using a linked list.

    Maintain front and rear pointers; enqueue at rear, dequeue from front.

  32. Explain & vs && in C.

    & is bitwise AND, && is logical AND (short-circuits on first false).

  33. Call by value vs call by reference.

    Call by value passes a copy; call by reference passes an address so changes affect the caller.

  34. Difference between exit() and return in main.

    exit() terminates the program immediately; return in main returns control to the OS after cleanup.

  35. What is the volatile keyword?

    volatile tells the compiler a variable can change outside normal flow (e.g., hardware registers), preventing optimizations.

  36. What is the static keyword?

    static inside a function preserves the variable between calls; at file scope, it limits visibility to the translation unit.

  37. Explain storage classes in C.

    auto, register, static, extern control variable lifetime and linkage.

  38. How is typedef used in C?

    typedef creates an alias for existing types, improving readability (e.g., typedef unsigned long ulong;).

  39. Recursion vs iteration.

    Recursion solves by self-calls, can be elegant but uses more stack; iteration uses loops, often more memory-efficient.

  40. Difference between break and continue.

    break exits the loop entirely; continue skips to the next iteration.

  41. Explain preprocessor directives.

    Directives like #include, #define, #if control compilation before actual code compilation.

  42. Macro vs inline function.

    Macros are text substitutions; inline functions are type-checked and avoid function-call overhead without macro pitfalls.

  43. File I/O in C.

    Use fopen, fread/fwrite or fprintf/fscanf, and fclose for reading/writing files.

  44. How to read and write binary files?

    Open with "rb"/"wb" modes and use fread/fwrite for structured data.

  45. Explain command line arguments.

    main(int argc, char *argv[]) receives argument count and array of argument strings.

  46. Difference between argc and argv.

    argc is the number of arguments; argv is an array of pointers to argument strings.

  47. What are I/O buffers?

    Buffers temporarily store data for I/O operations to improve efficiency by reducing system calls.

  48. Text vs binary files.

    Text files store human-readable characters; binary files store raw bytes without encoding.

  49. How to handle signals in C?

    Use signal() or sigaction() to register a handler function for SIGINT, SIGSEGV, etc.

  50. Explain the extern keyword.

    extern declares a variable defined in another file, enabling cross-file linkage.

  51. What is function recursion depth limit?

    Recursion depth is limited by available stack size; deep recursion can cause stack overflow.

  52. Difference between FILE* and file descriptor.

    FILE* is a C standard I/O handle with buffering; file descriptor (int) is a low-level OS handle.