C interviews often focus on memory layout, pointer manipulation, and hardware interaction. Unlike high-level language interviews, C interviews test your understanding of what happens "under the hood."
#include <file.h> and #include "file.h"?The difference lies in the search path the preprocessor follows:
stdio.h reside).Storage classes define the scope, default value, and lifetime of a variable.
| Class | Storage | Lifetime |
|---|---|---|
| auto | Stack | Function block |
| static | Data Segment | Program duration |
| extern | Data Segment | Program duration |
| register | CPU Register | Function block |
Both are dangerous but have different origins:
free()'d or the variable has gone out of scope.malloc() and calloc().While both allocate memory on the Heap, they differ in initialization:
volatile keyword used for?
The volatile keyword tells the compiler not to optimize the variable because its value can change unexpectedly outside the control of the program (e.g., a hardware register, an interrupt service routine, or a variable shared by multiple threads). Without volatile, the compiler might assume the value hasn't changed and read a "stale" value from a CPU register instead of RAM.
struct contain a pointer to itself?Yes, this is called a Self-Referential Structure. It is the basis for linked lists, trees, and graphs.
A memory leak occurs when a programmer allocates memory on the heap (using malloc/calloc) but fails to release it using free() after it is no longer needed. Over time, the program consumes more and more RAM until it crashes or slows down the system.
Prevention: Always pair every allocation with a free() and use tools like Valgrind to detect leaks during development.
++i and i++?You can use the Bitwise XOR operator to perform the swap efficiently:
const qualifier?
The const keyword ensures that a variable's value cannot be modified after initialization. However, in C, you must be careful with pointers:
const int *ptr; : You cannot change the value pointed to.int * const ptr; : You cannot change the address stored in the pointer.malloc doesn't initialize memory, explain that it's designed that way for maximum performance in high-speed applications.