Top 50 C Interview Questions
1. What is recursion in C?
Recursion is when a function calls itself directly or indirectly to solve a problem.
2. What is the use of the static keyword in C?
The static keyword preserves the value of a variable between function calls.
3. What is the difference between global and static global variables?
Global variables can be accessed across files, static global variables are limited to the file in which they are defined.
4. What is the difference between const and #define?
const is a typed constant, while #define is a preprocessor directive.
5. What is a pointer in C?
A pointer is a variable that stores the address of another variable in memory.
6. Explain the difference between malloc() and calloc() in C?
malloc() allocates a block of memory without initializing it, while calloc() allocates memory and initializes it to zero.
7. What is a structure in C?
A structure is a user-defined data type that can hold multiple data types in a single unit.
8. What is the purpose of the ‘break’ statement in C?
The ‘break’ statement is used to exit from loops or switch cases prematurely.
9. What is the purpose of the ‘continue’ statement in C?
The ‘continue’ statement is used to skip the current iteration of a loop and move to the next iteration.
10. What is a linked list in C?
A linked list is a linear data structure in which elements are stored in nodes, where each node points to the next node in the list.
11. Explain the difference between array and linked list?
An array is a collection of elements stored in contiguous memory locations, while a linked list consists of nodes where each node points to the next one.
12. What is dynamic memory allocation in C?
Dynamic memory allocation allows allocating memory during runtime using functions like malloc(), calloc(), realloc(), and free().
13. What is the purpose of the ‘sizeof’ operator in C?
The ‘sizeof’ operator is used to get the size of a data type or a variable in bytes.
14. What is a function pointer in C?
A function pointer is a pointer that stores the address of a function, enabling dynamic function calls.
15. What is the purpose of ‘#include’ directive in C?
The ‘#include’ directive is used to include the contents of a file (like header files) into a C program.
16. 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.
17. 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.
18. 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.
19. 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.
20. 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.
21. 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().
22. 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().
23. Describe the sizeof operator.
sizeof returns the size in bytes of a data type or object on the target platform.
24. 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.
25. 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--].
26. What are function pointers and how are they used?
Function pointers store addresses of functions and allow callbacks, dynamic dispatch, and function tables.
27. Explain bitwise operators and their use cases.
Operators like &, |, ^, ~, <<, >> operate on individual bits, useful for flags, masks, encryption, and performance-critical code.
28. 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).
29. 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.
30. What is a segmentation fault?
A segmentation fault occurs when a program accesses memory it’s not allowed to, usually due to invalid pointers.
31. 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).
32. 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).
33. Implement a C program to reverse a string without built-ins.
Swap characters from ends moving toward the center using two indices.
34. How would you find the maximum element in an array?
Iterate through the array, tracking the largest value seen so far.
35. Write a C program to check if a number is a palindrome.
Reverse the number by extracting digits and compare with the original.
36. 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.
37. How do you remove duplicates from an array?
Use nested loops or a hash table to track seen elements and shift unique elements forward.
38. 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.
39. Write a C program to check if two strings are anagrams.
Count frequency of each character in both strings and compare the counts.
40. Reverse the words in a given string.
Split the string by spaces, reverse the word array, then rejoin with spaces.
41. 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.
42. Merge two sorted linked lists.
Iteratively compare head nodes, attach the smaller to the merged list, advance pointer.
43. 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.
44. 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.
45. Swap two numbers without a temporary variable.
Use XOR: a ^= b; b ^= a; a ^= b.
46. What is the difference between a stack and a queue?
A stack is LIFO (Last In First Out), while a queue is FIFO (First In First Out).
47. What is a binary tree?
A binary tree is a hierarchical structure where each node has at most two children.
48. How do you traverse a binary tree?
In-order, pre-order, and post-order are common traversal methods.
50. What is the difference between call by value and call by reference?
In call by value, a copy of the actual parameter is passed to the function, while in call by reference, the memory address of the actual parameter is passed.