1. Control flow — decisions & loops
if/else, ternary? :,switch(falls through unlessbreak),for/while/do-while.switchon int/enum; duplicate/literal cases are a compile error; default clause is optional.do whileruns the block first, then checks the condition at least once.gotoexists — used for cleanup in C and some kernels only.
Classic output trap: break only exits the current switch/loop; falling through a case without break continues executing the next case.
2. Recursion — the placement favorite
int fact(int n) { return n <= 1 ? 1 : n * fact(n - 1); }
int fib(int n) { return n < 2 ? n : fib(n - 1) + fib(n - 2); }
Hazards to flag quickly in interviews:
- Base case missing = infinite recursion → stack overflow.
- Fibonacci naive is exponential — memoise with an array.
- Tail recursion in C is not guaranteed optimized.
3. I/O and stdlib — the standard toolkit
printf/scanffamily:%d,%f,%c,%s,%p,%x,%zu(size_t);%sprints from a char* until\0.- Format-string mismatch == UB, often the first out-of-range diagnosis (
%fprinting int etc.). exit()vs_exit();atoi/strtol(strtol usually better error handling).qsort/bsearchwith comparator functions onconst void *.
int cmp(const void *a, const void *b) { return *(int*)a - *(int*)b; }
qsort(arr, n, sizeof(int), cmp);
4. Undefined behaviour — the top-5 interview list
- Dereference
NULL/ wild pointers. - Out-of-bounds array access (read or write).
- Use of a dangling pointer (after
free/ after stack frame ends). - Signed integer overflow (
INT_MAX + 1). - Shifting by
>= widthof the type.
Some look “normal” but are UB: bad printf format, strcpy overflow, modifying a string literal, reading uninitialized data.
5. The gotcha sheet
sizeofcomputed at compile time — not a function;sizeof(array)vssizeof(pointer).- Integer division truncates toward zero in C99+:
7/2 == 3. - Ternary type promotion surprises.
- Comma in expression vs function-arg list:
a, bvsf(a, b). &&||short-circuit — the right operand side doesn’t run when result is known.staticlocal: initialized once, persists across calls.unsignedarithmetic wraps fast; mixing signed/unsigned is a minefield.
6. Interview checkpoint
- Recursion base/hazards; stack depth.
- printf/scanf format correctness —
%zu,%p,%x. - Spotting UB: run UBSan/valgrind to confirm your candidate list.
- The gotcha sheet above — say it once, cleanly.
Premium Content
Unlock Part 4: Control Flow, Stdlib, Undefined Behaviour & Gotchas and all premium lessons with a subscription.
All premium lessons
Ad-free experience
Priority support
From ₹199.99/year — See plans