Top 25 C Language Interview Questions
Curated questions covering pointers, memory management, data types, structures, file I/O, and C fundamentals.
What is C and what are its key features?
C is a general-purpose, procedural programming language developed by Dennis Ritchie. Key features: low-level memory access via pointers, efficient performance, portability, structured programming, and it forms the basis for many modern languages (C++, Java, Python).
What is the difference between a compiler and an interpreter?
A compiler translates the entire source code to machine code before execution (C uses a compiler). An interpreter translates and executes code line by line. Compiled programs are generally faster; interpreted programs are more flexible.
What are the basic data types in C?
- int — integer (typically 4 bytes)
- char — single character (1 byte)
- float — single-precision floating point (4 bytes)
- double — double-precision floating point (8 bytes)
- void — no value
- Modifiers: short, long, signed, unsigned
What is a pointer in C?
A pointer is a variable that stores the memory address of another variable. Pointers enable dynamic memory allocation, array manipulation, and passing variables by reference.
int x = 10;
int *ptr = &x; // ptr holds address of x
printf("%d", *ptr); // dereference: prints 10
*ptr = 20; // modifies x through pointer
What is the difference between call by value and call by reference?
- Call by value — a copy of the argument is passed; changes inside the function do not affect the original.
- Call by reference — the address of the argument is passed (using pointers); changes inside the function affect the original.
void byValue(int x) { x = 100; } // original unchanged
void byRef(int *x) { *x = 100; } // original changed
What is the difference between malloc, calloc, realloc, and free?
- malloc(size) — allocates size bytes; memory is uninitialized.
- calloc(n, size) — allocates n*size bytes; initializes to zero.
- realloc(ptr, size) — resizes previously allocated memory.
- free(ptr) — releases allocated memory back to the heap.
int *arr = (int*)malloc(5 * sizeof(int));
int *arr2 = (int*)calloc(5, sizeof(int)); // zero-initialized
arr = (int*)realloc(arr, 10 * sizeof(int));
free(arr);
What is the difference between stack and heap memory?
- Stack — automatic memory for local variables and function calls; LIFO; limited size; automatically freed.
- Heap — dynamic memory allocated with malloc/calloc; larger; must be manually freed with free().
What are arrays in C?
An array is a contiguous block of memory storing elements of the same type. Array name is a pointer to the first element. Arrays are zero-indexed and have fixed size.
int arr[5] = {1, 2, 3, 4, 5};
printf("%d", arr[2]); // 3
printf("%p", arr); // address of first element
What is a structure in C?
A structure (struct) groups variables of different types under a single name. Used to represent complex data types.
struct Student {
char name[50];
int age;
float gpa;
};
struct Student s = {"Alice", 20, 3.8};
printf("%s is %d years old", s.name, s.age);
What is the difference between struct and union?
- struct — each member has its own memory; total size = sum of all members.
- union — all members share the same memory; total size = size of largest member.
- Use union when only one member is needed at a time to save memory.
What are storage classes in C?
- auto — default for local variables; stored on stack.
- register — suggests storing in CPU register for faster access.
- static — persists between function calls; local scope but permanent storage.
- extern — declares a variable defined in another file.
What is a function pointer in C?
A function pointer stores the address of a function and can be used to call it indirectly. Useful for callbacks and implementing polymorphism in C.
int add(int a, int b) { return a + b; }
int (*fp)(int, int) = add;
printf("%d", fp(3, 4)); // 7
What is the difference between #define and const?
- #define — preprocessor macro; no type checking; replaced before compilation; no memory allocated.
- const — typed constant; type-checked by compiler; stored in memory; preferred in modern C.
What is the difference between while, do-while, and for loops?
- for — best when number of iterations is known.
- while — checks condition before each iteration; may not execute at all.
- do-while — checks condition after each iteration; always executes at least once.
What are string functions in C?
C strings are null-terminated char arrays. Key functions from string.h: strlen() (length), strcpy() (copy), strcat() (concatenate), strcmp() (compare), strstr() (find substring), sprintf() (format to string).
What is the difference between gets() and fgets()?
gets() reads a line but does not check buffer size — vulnerable to buffer overflow (deprecated). fgets() reads up to n-1 characters and is safe. Always use fgets() for user input.
char buf[100];
fgets(buf, sizeof(buf), stdin); // safe
What is a dangling pointer?
A dangling pointer points to memory that has been freed or gone out of scope. Dereferencing it causes undefined behaviour. Always set pointers to NULL after freeing.
int *p = (int*)malloc(sizeof(int));
free(p);
p = NULL; // prevent dangling pointer
What is a memory leak?
A memory leak occurs when dynamically allocated memory is never freed. Over time it exhausts available memory. Always pair malloc/calloc with free(). Use tools like Valgrind to detect leaks.
What is the difference between ++i and i++?
++i (pre-increment) increments i and returns the new value. i++ (post-increment) returns the current value and then increments. In most contexts they produce the same result, but differ when used in expressions.
What are bitwise operators in C?
- & (AND), | (OR), ^ (XOR), ~ (NOT), << (left shift), >> (right shift).
- Used for low-level bit manipulation, flags, and performance optimisations.
int flags = 0b0000;
flags |= (1 << 2); // set bit 2
flags &= ~(1 << 2); // clear bit 2
int isSet = flags & (1 << 2); // test bit 2
What is the sizeof operator?
sizeof returns the size in bytes of a type or variable at compile time. It is not a function — it is a compile-time operator. Essential for portable code and dynamic memory allocation.
What is recursion in C?
Recursion is when a function calls itself. Every recursive function needs a base case to stop. Used for problems like factorial, Fibonacci, and tree traversal.
int factorial(int n) {
if (n <= 1) return 1; // base case
return n * factorial(n - 1); // recursive case
}
What is file I/O in C?
C provides file operations through stdio.h. Key functions: fopen() (open), fclose() (close), fprintf()/fscanf() (formatted I/O), fread()/fwrite() (binary I/O), fgets()/fputs() (line I/O).
FILE *fp = fopen("data.txt", "r");
if (fp) {
char line[256];
while (fgets(line, sizeof(line), fp)) printf("%s", line);
fclose(fp);
}
What is the difference between local and global variables?
- Local — declared inside a function; accessible only within that function; stored on stack; created/destroyed with function call.
- Global — declared outside all functions; accessible throughout the program; stored in data segment; persist for program lifetime.
What is the preprocessor in C?
The C preprocessor processes directives before compilation. Common directives: #include (include files), #define (macros), #ifdef/#ifndef (conditional compilation), #pragma (compiler-specific instructions).