Prepare for Your Next C Developer Role with These Essential Questions
Mastering C programming is crucial for roles at companies like Amazon, Zoho, Paytm, and Atlassian. This comprehensive guide covers 30 essential C interview questions arranged by difficulty – from basic concepts for freshers to advanced topics for experienced developers.
Basic Level C Interview Questions (1-10)
1. What is the difference between #include <stdio.h> and #include "stdio.h"?
The angle brackets <> tell the preprocessor to search in system/standard directories first, while double quotes "" search in the current directory first, then system directories. Use <> for standard headers and "" for custom headers.
2. Can you compile a C program without a main() function?
Yes, you can compile it, but it cannot be executed because program execution starts from main(). The compiler won’t throw an error during compilation, but linking/execution will fail.
3. What are the differences between malloc() and calloc()?
malloc() allocates memory without initializing it (contains garbage values), while calloc() allocates memory and initializes all bytes to zero. malloc() takes one argument (size), calloc() takes two (number of elements, size per element).
4. What is a pointer?
A pointer is a variable that stores the memory address of another variable. Pointers enable direct memory access and are essential for dynamic memory allocation and data structures.
int x = 10;
int *ptr = &x; // ptr holds address of x
5. What is the difference between call by value and call by reference in C?
Call by value passes a copy of the variable (original unchanged). Call by reference passes the address using pointers (original can be modified).
// Call by value
void swap(int a, int b) { int temp = a; a = b; b = temp; }
// Call by reference
void swap(int *a, int *b) { int temp = *a; *a = *b; *b = temp; }
6. What is recursion in C?
Recursion is when a function calls itself to solve smaller instances of the same problem. It needs a base case to stop infinite calls. Example: calculating factorial.
int factorial(int n) {
if (n <= 1) return 1; // base case
return n * factorial(n-1);
}
7. What are local and global variables?
Local variables are declared inside a function (scope limited to that function). Global variables are declared outside functions (accessible throughout the file after declaration).
8. What is the purpose of the const keyword?
const creates variables that cannot be modified after initialization. It improves code safety and readability.
const int MAX_SIZE = 100;
9. What are the storage classes in C?
auto (local variables), register (stored in CPU registers), static (retains value between calls), extern (declares variable defined elsewhere).
10. How do you create an infinite loop in C?
Use for(;;), while(1), or do { } while(1). These never evaluate to false.
for(;;) {
// infinite loop
}
Intermediate Level C Interview Questions (11-20)
11. What is a dangling pointer?
A dangling pointer points to memory that has been freed or deallocated. Accessing it causes undefined behavior or crashes.
int *ptr = malloc(sizeof(int));
free(ptr);
*ptr = 10; // Dangling pointer - undefined behavior
12. Explain the memory layout of a C program.
Memory is divided into: Text segment (code), Data segment (initialized global/static), BSS segment (uninitialized global/static), Heap (dynamic allocation), Stack (function calls, local variables).
13. What is a memory leak and how to avoid it?
Memory leak occurs when dynamically allocated memory is not freed. Avoid by always calling free() for every malloc()/calloc(), and set pointers to NULL after freeing.
14. What is the difference between struct and union?
struct allocates memory for all members separately. union allocates memory for the largest member only; all members share the same memory location.
15. Write a C program to find the largest among three numbers.
#include <stdio.h>
int main() {
int a, b, c;
scanf("%d %d %d", &a, &b, &c);
int largest = (a > b) ? (a > c ? a : c) : (b > c ? b : c);
printf("Largest: %d\n", largest);
return 0;
}
16. How do you check if a number is prime?
Check divisibility from 2 to sqrt(n). If no divisors found, it's prime.
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;
}
17. What is the volatile keyword?
volatile tells the compiler that a variable's value can change unexpectedly (hardware registers, interrupts). Prevents compiler optimization of that variable.
18. What is a segmentation fault?
Segmentation fault occurs when accessing invalid memory (NULL pointer dereference, array out of bounds, freed memory access).
int *ptr = NULL;
*ptr = 10; // Segmentation fault
19. Write a program to reverse a string without using library functions.
void reverseString(char *str) {
int len = 0;
while(str[len] != '\0') len++;
for(int i = 0; i < len/2; i++) {
char temp = str[i];
str[i] = str[len-1-i];
str[len-1-i] = temp;
}
}
20. What are preprocessor directives?
Lines starting with # (like #include, #define, #ifdef) processed before compilation. Used for macros, conditional compilation, file inclusion.
Advanced Level C Interview Questions (21-30)
21. What is the difference between macros and functions?
Macros are text substitution (no type checking, faster), functions involve overhead (type checking, stack frame). Macros can cause side effects with multiple evaluations.
22. Explain lvalue and rvalue.
lvalue: expression with memory address (can appear on left of assignment). rvalue: value/temporary (cannot appear on left).
int a = 10; // a is lvalue
a = 20; // 20 is rvalue
10 = 20; // Error - rvalue on left
23. Write a program to implement binary search.
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;
}
24. What is a function pointer?
Pointer that stores address of a function. Enables dynamic function calls.
int add(int a, int b) { return a + b; }
int main() {
int (*funcPtr)(int, int) = add;
int result = funcPtr(5, 3); // Calls add()
}
25. How does fprintf() and fscanf() work with structures?
You can read/write entire structures using fread()/fwrite() in binary mode.
fwrite(&student, sizeof(student), 1, file);
fread(&student, sizeof(student), 1, file);
26. What are command line arguments?
Arguments passed to main() via command line: int main(int argc, char *argv[]). argc is argument count, argv is argument vector.
27. Implement strcpy without using library functions.
void myStrcpy(char *dest, const char *src) {
while(*src != '\0') {
*dest = *src;
dest++;
src++;
}
*dest = '\0';
}
28. What is the __STDC__ macro?
Predefined macro indicating ANSI C compliance. If defined, compiler follows ANSI C standards.
#ifdef __STDC__
printf("ANSI C compliant\n");
#endif
29. Write a macro to find maximum of two numbers.
#define MAX(a, b) ((a) > (b) ? (a) : (b))
Note: Parentheses prevent precedence issues and side effects.
30. Detect cycle in a linked list (scenario: Atlassian coding round).
Use Floyd's cycle detection (two pointers - slow moves 1 step, fast moves 2 steps). If they meet, cycle exists.
int hasCycle(struct Node* head) {
struct Node *slow = head, *fast = head;
while(fast && fast->next) {
slow = slow->next;
fast = fast->next->next;
if(slow == fast) return 1;
}
return 0;
}
Practice these questions regularly to excel in C programming interviews at top companies. Focus on understanding concepts deeply and writing clean, efficient code.