Interview Q&A · C · 8 questions in this topic
Operators & Control Flow — C Interview Q&A
Bitwise operators, precedence traps, short-circuiting, and the control-flow edge cases interviews probe. Part of Data Types & Operators · 202 questions in the C bank.
What are the operator families in C?
Answer
Arithmetic (+ - * / %), relational and equality (== != < >), logical (&& || !), bitwise (& | ^ ~ << >>), assignment (= and compound += etc.), increment/decrement (++ --), conditional (?:), comma (,), member access (. ->), plus sizeof, casts and address/deref (& *).
Two worth calling out: / on integers truncates toward zero, and % takes the sign of the dividend.
Example
#include <stdio.h> int main(void) { printf("%d %d\n", 7 / 2, 7 % 2); /* 3 and 1 */ printf("%d %d\n", -7 / 2, -7 % 2); /* -3 and -1 (C99 rule) */ return 0; }
💡 State the C99 truncation rule for negatives — candidates from other languages routinely get -7/2 wrong.
What is the difference between `&` / `|` and `&&` / `||`?
Answer
Bitwise & | ^ operate on every bit of their operands and always evaluate both sides. Logical && || treat operands as true/false, short-circuit (the right side may never run), and yield 0 or 1.
The classic bug is typing & for && in a condition — it compiles, runs both sides, and produces a bitmask instead of a boolean.
Example
int a = 1, b = 2; /* if (a & b) // bitwise: 1 & 2 == 0 -> false (bug!) */ if (a && b) /* logical: both nonzero -> true */ ;
What is the difference between `>>` on signed vs unsigned integers?
Answer
Right-shifting unsigned (or non-negative signed) values is logical: zeros shift in. Right-shifting negative signed values is implementation-defined (arithmetic shift on virtually all platforms: the sign bit replicates), and shifting by >= width or shifting negative values left is undefined behavior.
Portable bit code uses unsigned types exclusively and never shifts into/through the sign bit.
Example
#include <stdio.h> int main(void) { unsigned u = 8u; /* 1000b */ printf("%u\n", u >> 2); /* 2: zeros shifted in */ return 0; }
💡 Unsigned for bit work is the review-proof rule; cite it and move on.
What is operator precedence trap: `*p++`, `a = b == c`, `x & y == z`?
Answer
Postfix ++/-- bind tightest: *p++ parses as *(p++) — dereference the current pointer, then advance it. == outranks =: a = b == c assigns the comparison result. == outranks &: x & y == z parses as x & (y == z), almost never what was meant.
Parenthesize bit operations and assignments inside conditions explicitly — -Wparentheses exists because these bugs are endemic.
Example
/* copy idiom that depends on precedence: */ /* while (*dst++ = *src++) ; // *(dst++) = *(src++) */ /* if ((x & MASK) == EXPECTED) // parens required */
💡 Memorize the three classic misparses (*p++, = vs ==, & vs ==) — they are evergreen interview material.
How do `break`, `continue`, `goto` and early return compare?
Answer
break exits the innermost loop/switch; continue skips to the next iteration; goto jumps anywhere in the function ( legitimate in C for unified cleanup on error paths); early return exits the function at a guard clause.
C has no exceptions or defer, so `goto cleanup` with one exit path is idiomatic error handling in resource-heavy functions — hated in other languages, standard practice here.
Example
int process(const char *path) { FILE *f = fopen(path, "r"); if (!f) return -1; int rc = -1; char *buf = malloc(4096); if (!buf) goto out; /* ... work ... */ rc = 0; out: free(buf); fclose(f); return rc; }
💡 Defend goto cleanup as the C-idiomatic pattern — interviewers testing C culture expect exactly that.
What is the difference between `if/else`, `switch`, and lookup tables?
Answer
if/else chains suit ranges and mixed conditions. switch dispatches on discrete integer/enum values with fall-through semantics (every case needs break unless fall-through is deliberate — -Wimplicit-fallthrough enforces the comment). Lookup tables (arrays of values or function pointers indexed by the key) beat long switches for dense mappings and state machines.
Modern C reviews flag missing breaks and switch-on-bool as smells.
Example
/* state machine via table instead of a 20-case switch: */ typedef void (*handler_t)(void); handler_t table[4] = { idle, run, pause, stop }; table[state]();
What is short-circuit evaluation and how is it used safely?
Answer
&& and || evaluate left-to-right and stop as soon as the result is decided: a && b skips b when a is false; a || b skips b when a is true. This guards dereferences (p && p->ready), bounds (i < n && a[i]) and chains fallbacks — but only with side-effect-free or intentionally-sequenced right sides.
Never hide required work in a short-circuited operand: if (ready && flush()) may never flush.
Example
/* safe guard pattern: */ /* if (p != NULL && p->len > 0) // p->len touched only if p valid */ /* if (i < n && a[i] == 0) // a[i] touched only in bounds */
💡 Guard-then-use is the pattern; skipped-side-effects is the hazard — state both halves.
What is the comma operator and why does it confuse people?
Answer
The comma operator evaluates left-to-right, discards the left value, and yields the right value — with a sequence point between. It shines in for-loop headers (for (i = 0, j = n-1; ...)) and confuses everywhere else, especially since commas separating function arguments are not the comma operator.
Interview relevance: `x = (a, b)` assigns b; `f(a, b)` passes two arguments — same glyph, different grammar.
Example
int x = (1, 2, 3); /* x == 3: left values discarded */ /* for (i = 0, j = n - 1; i < j; i++, j--) // idiomatic use */
Recommended resourcesAffiliate links · may earn us a commission