Prepare for Your C Interview: Basic to Advanced Questions
Mastering C programming is essential for IT roles at companies like Amazon, Zoho, and Atlassian. This comprehensive guide covers 30 essential interview questions arranged by difficulty level – from fresher basics to advanced concepts for 3-6 years experienced candidates. Each question includes clear explanations and practical code examples.
Basic C Interview Questions (Freshers)
1. What is the basic structure of a C program?
A C program starts with #include directives, followed by the main() function containing executable statements within curly braces.
#include <stdio.h>
int main() {
printf("Hello World!");
return 0;
}
2. What is the difference between a variable and a constant?
A variable can change its value during program execution, while a constant (declared with const) maintains the same value throughout.
3. What are the different data types in C?
C supports basic data types: int, char, float, double, and modifiers like short, long, signed, unsigned.
4. What is the difference between printf() and scanf()?
printf() outputs formatted data to console, while scanf() reads formatted input from console.
5. Can a C program compile without a main() function?
Yes, it can compile but cannot execute, as program execution always starts from main().
6. What is a preprocessor directive?
Lines starting with # (like #include, #define) processed before compilation to include files or define macros.
7. What is the size of int in C?
Typically 4 bytes (32 bits) on modern systems, but implementation-dependent. Use sizeof(int) to check.
8. Write a program to swap two numbers without using a temporary variable.
#include <stdio.h>
int main() {
int a = 10, b = 20;
a = a + b;
b = a - b;
a = a - b;
printf("a=%d, b=%d", a, b);
return 0;
}
9. What are actual and formal parameters?
Actual parameters are values passed to function during call. Formal parameters are variables defined in function declaration/definition.
10. What is the default function calling method in C?
Functions are called by value by default, passing copies of arguments.
Intermediate C Interview Questions (1-3 Years Experience)
11. What is a pointer in C?
A pointer is a variable storing memory address of another variable. Declared as int *ptr;.
12. What is the difference between call by value and call by reference?
Call by value passes argument copies (original unchanged). Call by reference passes pointer to argument (original modified).
void swap(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}
13. What are the differences between malloc() and calloc()?
| malloc() | calloc() |
|---|---|
| Allocates single block | Allocates multiple blocks |
| Memory uninitialized | Memory initialized to zero |
ptr = malloc(size); |
ptr = calloc(n, size); |
14. What is a memory leak in C?
Memory leak occurs when dynamically allocated memory using malloc()/calloc() is not freed using free(), causing memory wastage.
15. Write a program to find the largest number among three numbers.
#include <stdio.h>
int main() {
int a, b, c;
scanf("%d %d %d", &a, &b, &c);
if(a >= b && a >= c)
printf("%d is largest", a);
else if(b >= c)
printf("%d is largest", b);
else
printf("%d is largest", c);
return 0;
}
16. What is recursion in C?
Recursion is when a function calls itself. Base case prevents infinite calls.
17. Write a recursive program to find factorial.
#include <stdio.h>
int factorial(int n) {
if(n <= 1) return 1;
return n * factorial(n-1);
}
18. What is the difference between struct and union?
| struct | union |
|---|---|
| Separate memory for each member | Shared memory (size of largest member) |
| All members accessible simultaneously | Only one member active at a time |
19. What is typecasting in C?
Typecasting converts one data type to another. Example: int num = (int)3.14;.
20. What are storage class specifiers?
auto, register, static, extern define variable scope, lifetime, and storage.
Advanced C Interview Questions (3-6 Years Experience)
21. What is a function pointer?
Pointer storing address of a function, enabling dynamic function calls.
int add(int a, int b) { return a+b; }
int main() {
int (*func_ptr)(int, int) = add;
int result = func_ptr(5, 3);
}
22. Explain the memory layout of a C program.
C program memory divided into: Text segment (code), Data segment (initialized globals), BSS segment (uninitialized globals), Heap (dynamic), Stack (local variables).
23. What causes a segmentation fault?
Accessing invalid memory like dereferencing NULL pointer, array bounds violation, or invalid pointer arithmetic.
24. Write a program to check if a number is prime.
#include <stdio.h>
int isPrime(int n) {
if(n <= 1) return 0;
for(int i=2; i*i<=n; i++)
if(n%i == 0) return 0;
return 1;
}
25. What is the volatile keyword?
volatile tells compiler variable may change unexpectedly (hardware registers, interrupts), preventing optimization.
26. Difference between array and pointer?
| Array | Pointer |
|---|---|
| Fixed size at compile time | Can point to dynamic memory |
| Contiguous memory block | Single address |
| Decays to pointer in expressions | Can be reassigned |
27. Write a program to reverse a string without using library functions.
#include <stdio.h>
void reverse(char *str) {
int len = 0;
while(str[len]) len++;
for(int i=0; i
28. What are l-value and r-value?
l-value: Expression with memory address (assignable). r-value: Expression representing value (not assignable).
29. Implement binary search in C.
int binarySearch(int arr[], int size, int key) {
int left = 0, right = size-1;
while(left <= right) {
int mid = left + (right-left)/2;
if(arr[mid] == key) return mid;
if(arr[mid] < key) left = mid+1;
else right = mid-1;
}
return -1;
}
30. What is a double pointer? Give an example.
Pointer pointing to another pointer. Used in dynamic 2D arrays or modifying pointers in functions.
void allocate2DArray(int ***arr, int rows, int cols) {
*arr = (int**)malloc(rows * sizeof(int*));
for(int i=0; i
Practice these 30 C programming interview questions to excel in technical interviews at product companies, SaaS platforms, and startups. Focus on understanding concepts deeply and writing clean, efficient code.