C interviews focus on how well you understand memory, pointers and what the compiler really does. These questions are grouped by topic and level, each with a short, clear answer.
What C Interviewers Look For
Why C Interview Questions Matter
- Foundation assessment: Evaluate core programming concepts.
- Memory management understanding: Pointers, allocation, deallocation.
- Problem-solving skills: Algorithm implementation and optimization.
- Systems programming knowledge: Low-level operations.
- Code quality: Proper error handling and best practices.
Categories of Interview Questions
| Level | Topics Covered |
|---|---|
| Beginner | Basic syntax, data types, loops, functions |
| Intermediate | Pointers, memory management, strings, structures |
| Advanced | Linked lists, recursion, file operations, optimization |
Key Areas to Master
- Memory management:
malloc,calloc,realloc,free - Pointers: Pointer arithmetic, double pointers, void pointers
- Data structures: Arrays, structures, unions, linked lists
- Strings: String functions, character arrays
- File I/O: Reading, writing, error handling
- Recursion: Base cases, stack usage
- Preprocessor: Macros, header files, conditional compilation
- Storage classes:
static,extern,register,auto
Beginner Level Interview Questions
1. Why is C called a mid-level programming language?
C combines features of both low-level and high-level languages:
- Low-level: Direct memory access via pointers, bit manipulation.
- High-level: Functions, loops, conditionals, structured programming.
2. What are the basic data types in C?
| Data Type | Description |
|---|---|
int | Integer values |
char | Single character |
float | Single-precision floating-point |
double | Double-precision floating-point |
void | Absence of value |
3. What are tokens in C?
Tokens are the smallest meaningful units:
- Keywords:
int,if,return - Identifiers: Variable/function names
- Constants: Fixed values
- Strings: Character sequences
- Operators:
+,-,* - Special symbols:
(),{},;
4. What is the scope of a variable?
Scope defines where a variable is accessible:
- Local: Inside a function/block
- Global: Throughout the program
- Formal parameters: Function parameter list
5. What are static variables?
Static variables retain their value between function calls and are initialized only once.
#include <stdio.h>
void countCalls(void) {
static int count = 0; // Initialized once
count++;
printf("%d\n", count);
}
int main(void) {
countCalls(); // 1
countCalls(); // 2
countCalls(); // 3
return 0;
}
Expected output:
1 2 3
6. Difference between malloc() and calloc()
malloc() | calloc() |
|---|---|
| Single block of memory | Multiple contiguous blocks |
| One argument (total size) | Two arguments (count, size) |
| Uninitialized (garbage) | Initialized to zero |
| Faster | Slightly slower |
7. What are dangling pointers?
A dangling pointer points to memory that has already been freed.
int *ptr = malloc(sizeof(int)); free(ptr); // ptr is now dangling ptr = NULL; // Fix: set to NULL
8. Difference between local and global variables
| Local | Global |
|---|---|
| Declared inside a function/block | Declared outside all functions |
| Garbage value by default | Zero by default |
| Destroyed after block | Exists for program lifetime |
| Stack storage | Data segment storage |
9. What are pointers?
Pointers store memory addresses of other variables.
int x = 10;
int *ptr = &x; // ptr stores address of x
printf("%d\n", *ptr); // 10 (dereference)
10. What is typedef?
typedef creates an alias for an existing data type.
typedef long long ll; ll num = 1000000000;
Intermediate Level Interview Questions
11. Difference between strcpy() and strncpy()
strcpy() | strncpy() |
|---|---|
| Copies entire string | Copies up to n characters |
Always appends '\0' | May not append '\0' |
| Risk of buffer overflow | Safer (limited copy) |
#include <stdio.h>
#include <string.h>
int main(void) {
char src[] = "GeeksforGeeks";
char dest[20];
strncpy(dest, src, 5);
dest[5] = '\0'; // Ensure null termination
printf("%s\n", dest); // Geeks
return 0;
}
12. What is pointer arithmetic?
Pointer arithmetic moves pointers by the size of the data type.
#include <stdio.h>
int main(void) {
int arr[] = {10, 20, 30};
int *ptr = arr;
printf("%d\n", *ptr); // 10
ptr++;
printf("%d\n", *ptr); // 20
return 0;
}
13. What is a double pointer?
A double pointer stores the address of another pointer.
int num = 10;
int *ptr = #
int **dptr = &ptr;
printf("%d\n", **dptr); // 10
14. What is a void pointer?
A void pointer can store any address but must be cast before dereferencing.
int num = 10;
void *ptr = #
printf("%d\n", *(int *)ptr); // 10
15. Difference between stack and heap memory
| Stack | Heap |
|---|---|
| Automatic allocation | Manual allocation (malloc/free) |
| Local variables | Dynamic data |
| Faster | Slower |
| Limited size | Larger size |
16. How to convert string to number?
Use atoi, strtol, or sscanf.
#include <stdio.h>
#include <stdlib.h>
int main(void) {
char str[] = "123";
int num = atoi(str);
printf("%d\n", num); // 123
return 0;
}
17. What is a structure?
A structure groups variables of different types.
struct Student {
char name[50];
int roll;
float marks;
};
18. What is a union?
A union shares memory among its members.
union Data {
int i;
float f;
};
/* sizeof(union Data) = sizeof(float) or sizeof(int), whichever is larger */
19. Difference between call by value and call by reference
| Call by Value | Call by Reference |
|---|---|
| Passes copy of value | Passes address |
| Original unchanged | Original can be modified |
| Simple | Requires pointers |
20. What are command-line arguments?
Arguments passed to a program at execution.
int main(int argc, char *argv[]) {
printf("Argument count: %d\n", argc);
for (int i = 0; i < argc; i++) {
printf("argv[%d]: %s\n", i, argv[i]);
}
return 0;
}
Advanced Level Interview Questions
21. Write a program to print Fibonacci series using recursion
#include <stdio.h>
int fibonacci(int n) {
if (n <= 1) return n;
return fibonacci(n - 1) + fibonacci(n - 2);
}
int main(void) {
for (int i = 0; i < 10; i++) {
printf("%d ", fibonacci(i));
}
printf("\n");
return 0;
}
Expected output:
0 1 1 2 3 5 8 13 21 34
22. Write a program to check if a number is prime
#include <stdio.h>
#include <math.h>
#include <stdbool.h>
bool isPrime(int n) {
if (n <= 1) return false;
if (n <= 3) return true;
for (int i = 2; i <= sqrt(n); i++) {
if (n % i == 0) return false;
}
return true;
}
int main(void) {
int num = 17;
if (isPrime(num)) {
printf("%d is prime\n", num);
} else {
printf("%d is not prime\n", num);
}
return 0;
}
23. Write a program to reverse a number
#include <stdio.h>
int main(void) {
int n = 12345;
int reversed = 0;
while (n != 0) {
reversed = reversed * 10 + n % 10;
n /= 10;
}
printf("Reversed: %d\n", reversed); // 54321
return 0;
}
24. What is the volatile keyword?
volatile tells the compiler the variable may change unexpectedly (hardware, interrupts).
volatile int flag = 0; /* Compiler always reads from memory, never optimizes */
25. What is a memory leak?
A memory leak occurs when allocated memory is not freed.
int *ptr = malloc(100); /* Missing free(ptr); */
Solution: Always free allocated memory.
Coding Questions With Answers in C
This program demonstrates several common interview topics in one comprehensive example.
Program: Interview Concepts Demo
/**
* interview_concepts.c
* Demonstrates common C interview topics:
* pointers, memory management, recursion, and string operations.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
/* Function prototypes */
void demonstratePointers(void);
void demonstrateMemoryManagement(void);
void demonstrateRecursion(void);
void demonstrateStringOperations(void);
bool isPalindrome(const char *str);
int factorial(int n);
void swapWithoutTemp(int *a, int *b);
int main(void) {
printf("=== C Interview Concepts Demo ===\n");
demonstratePointers();
demonstrateMemoryManagement();
demonstrateRecursion();
demonstrateStringOperations();
return 0;
}
/* Demonstrates pointer concepts */
void demonstratePointers(void) {
printf("\n--- Pointers ---\n");
int value = 42;
int *ptr = &value;
int **dptr = &ptr;
printf("Value: %d\n", value);
printf("Via pointer: %d\n", *ptr);
printf("Via double pointer: %d\n", **dptr);
/* Pointer arithmetic */
int arr[] = {10, 20, 30, 40, 50};
int *p = arr;
printf("Array traversal: ");
for (int i = 0; i < 5; i++) {
printf("%d ", *(p + i));
}
printf("\n");
}
/* Demonstrates memory management */
void demonstrateMemoryManagement(void) {
printf("\n--- Memory Management ---\n");
/* malloc */
int *arr = malloc(5 * sizeof(int));
if (arr == NULL) {
perror("malloc failed");
return;
}
for (int i = 0; i < 5; i++) {
arr[i] = i * 10;
}
/* realloc */
int *temp = realloc(arr, 10 * sizeof(int));
if (temp == NULL) {
perror("realloc failed");
free(arr);
return;
}
arr = temp;
printf("Memory allocated and resized successfully.\n");
/* free */
free(arr);
arr = NULL; // Prevent dangling pointer
printf("Memory freed.\n");
}
/* Demonstrates recursion */
void demonstrateRecursion(void) {
printf("\n--- Recursion ---\n");
printf("Factorial of 5: %d\n", factorial(5));
printf("Factorial of 0: %d\n", factorial(0));
}
/* Recursive factorial */
int factorial(int n) {
if (n <= 1) {
return 1; // Base case
}
return n * factorial(n - 1);
}
/* Demonstrates string operations */
void demonstrateStringOperations(void) {
printf("\n--- String Operations ---\n");
char str1[] = "racecar";
char str2[] = "hello";
printf("'%s' is %s\n", str1, isPalindrome(str1) ? "palindrome" : "not palindrome");
printf("'%s' is %s\n", str2, isPalindrome(str2) ? "palindrome" : "not palindrome");
/* Swap without third variable */
int a = 10, b = 20;
printf("\nBefore swap: a=%d, b=%d\n", a, b);
swapWithoutTemp(&a, &b);
printf("After swap: a=%d, b=%d\n", a, b);
}
/* Checks if a string is palindrome */
bool isPalindrome(const char *str) {
int start = 0;
int end = strlen(str) - 1;
while (end > start) {
if (str[start++] != str[end--]) {
return false;
}
}
return true;
}
/* Swaps two numbers without third variable */
void swapWithoutTemp(int *a, int *b) {
*a = *a + *b;
*b = *a - *b;
*a = *a - *b;
}
Expected Output
=== C Interview Concepts Demo === --- Pointers --- Value: 42 Via pointer: 42 Via double pointer: 42 Array traversal: 10 20 30 40 50 --- Memory Management --- Memory allocated and resized successfully. Memory freed. --- Recursion --- Factorial of 5: 120 Factorial of 0: 1 --- String Operations --- 'racecar' is palindrome 'hello' is not palindrome Before swap: a=10, b=20 After swap: a=20, b=10
Answers That Lose Interview Points
1. Not Checking malloc Return
Always check if memory allocation succeeded.
int *ptr = malloc(100); ptr[0] = 10; // BUG if malloc failed
Solution: Check if (ptr != NULL).
2. Memory Leaks
Forgetting to free allocated memory.
Solution: Always pair malloc with free.
3. Dangling Pointers
Using pointers after freeing.
Solution: Set pointer to NULL after free.
4. Buffer Overflow
Writing beyond array bounds.
Solution: Use bounds checking and safe functions.
5. Not Understanding Pointer Arithmetic
Confusing pointer addition with byte addition.
Solution: Remember: ptr + 1 moves by sizeof(*ptr) bytes.
6. Recursion Without Base Case
Causes stack overflow.
Solution: Always include base case.
7. Integer Overflow
Large arithmetic operations can overflow.
Solution: Use appropriate data types (long long, int64_t).
8. Not Null-Terminating Strings
Forgetting '\0' causes string function errors.
Solution: Ensure proper null termination.
9. Using == for String Comparison
Compares pointers, not contents.
Solution: Use strcmp.
10. Ignoring Compiler Warnings
Warnings often indicate real bugs.
Solution: Compile with -Wall -Wextra and fix warnings.
Final Interview Checklist
C interview questions test fundamental programming concepts, memory management, pointers, and problem-solving. Thorough preparation requires understanding both theory and practical application.
Key Takeaways
- Master pointers, memory management, and data structures.
- Understand recursion and its stack implications.
- Know string functions and their safe usage.
- Practice common algorithms (Fibonacci, prime, palindrome).
- Understand stack vs. heap memory.
- Be familiar with file I/O and error handling.
Best Practices for Interview Preparation
- Practice coding daily in C.
- Understand memory layout (stack, heap, data, text).
- Master pointer operations and pointer arithmetic.
- Write code with proper error handling.
- Learn common algorithms and their C implementations.
- Practice with previous interview questions.
- Understand time and space complexity.
- Review data structures (arrays, linked lists, trees).
- Test edge cases in your solutions.
- Explain your thought process clearly during interviews.
By mastering these concepts, you’ll be well-prepared for C programming interviews at all levels.
Related lessons
- C Functions Practice Problems with Solutions
- C Practice Problems: Arrays, Strings, Structs and Pointers
Want the full path? See every lesson in the C course, in order.
