Fifteen common C interview questions
- What is a pointer? What is a null pointer and a dangling pointer?
- What is the difference between an array and a pointer?
- Explain malloc, calloc, realloc and free.
- What is a memory leak, and how do you find one?
- What are the storage classes: auto, static, extern, register?
- What does static mean inside a function versus at file scope?
- Struct versus union?
- What is structure padding, and why does it happen?
- What does the volatile keyword do?
- What is undefined behaviour? Give examples.
- Macros versus inline functions?
- What does const mean in const int *p versus int *const p?
- How are arguments passed in C?
- What is a function pointer, and where is it used?
- How would you swap two variables, and what are the pitfalls of clever tricks?
Pointers and memory
- Pointer: a variable holding an address. A null pointer points nowhere valid; a dangling pointer points to memory that has been freed or gone out of scope.
- Array vs pointer: an array name decays to a pointer to its first element in most expressions, but sizeof(array) gives the whole array's size, and an array can't be reassigned.
- malloc allocates uninitialised memory; calloc allocates and zeroes; realloc resizes, possibly moving the block; free releases it. Always check for NULL and never use memory after freeing it.
- Leaks: memory allocated and never freed; detect with tools such as Valgrind or AddressSanitizer.
Storage, structs and qualifiers
| Concept | Answer |
|---|---|
| static in a function | The variable keeps its value between calls |
| static at file scope | Limits visibility to that file (internal linkage) |
| extern | Declares a variable defined in another file |
| Struct vs union | Members have separate storage vs members share the same storage |
| Padding | Compilers align members for efficient access, inserting unused bytes |
| volatile | The value may change outside the program's control, so don't optimise reads away |
| const int *p | Pointer to a constant int — can't change the value through p |
| int *const p | Constant pointer — can't change where p points |
Undefined behaviour
Undefined behaviour means the standard places no requirements on what happens, so the compiler may assume it never occurs and optimise accordingly. Common examples: dereferencing a null or dangling pointer, out-of-bounds array access, signed integer overflow, using an uninitialised variable, and modifying a variable twice without a sequence point (i = i++ + 1). In “what does this print?” questions, the correct answer to such code is that it's undefined — interviewers are often testing exactly that.
Bit manipulation basics
Systems and embedded interviews nearly always include a bit question. Know how to set a bit (x |= 1 << n), clear it (x &= ~(1 << n)), toggle it (x ^= 1 << n) and test it (x & (1 << n)); how to check for a power of two (x && !(x & (x - 1))); and why right-shifting a negative signed integer is implementation-defined. Counting set bits and reversing bits are common short coding tasks.
Macros, functions and passing arguments
C passes all arguments by value; to modify a caller's variable you pass its address. Macros are textual substitution by the preprocessor — no type checking, and arguments may be evaluated twice (a classic bug with SQUARE(i++)) — while inline functions are type-checked and evaluate arguments once. Function pointers enable callbacks, such as the comparison function passed to qsort, and dispatch tables in embedded systems. Embedded candidates should also see embedded software engineer questions.
Practising
The fastest way to find gaps is to be asked. Run a spoken mock round on C programming topics — a free AI interview assistant such as CoPilot Interview builds one from your résumé and the job description and scores every answer — then study whatever you stumbled on. Trace pointer snippets on paper with a box-and-arrow memory diagram; it prevents most mistakes. For C++ topics, see C++ interview questions.
Disclosure: CoPilot Interview is our product, so weigh what we say about it accordingly. Everything else on this page applies whichever tool you use, or none.
Frequently asked questions
What are the most common C interview questions?
Pointers, arrays versus pointers, dynamic memory allocation, memory leaks, storage classes, structs versus unions, padding, volatile, const placement, undefined behaviour, macros and function pointers.
What is the difference between malloc and calloc?
malloc allocates a block of uninitialised memory; calloc allocates memory for an array of elements and initialises every byte to zero.
What does volatile mean in C?
It tells the compiler that a variable's value may change outside the program's normal flow, such as a hardware register or a variable changed by an interrupt, so reads must not be optimised away.
What is undefined behaviour in C?
Code whose behaviour the C standard doesn't define, such as null dereferences, out-of-bounds access or signed overflow. The compiler may produce any result, including apparently working code.
Practise on real interview questions
Run 145 real interview questions across 10 tracks, answer out loud and see a structured answer to compare with yours. Free, no signup.
Try a practice question