Interview Q&A · C · 8 questions in this topic

Operators & Control Flow — C Interview Q&A

All C topics

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

Example.cc
#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

Example.cc
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

Example.cc
#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.

Recommended resourcesAffiliate links · may earn us a commission

Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Cloud hosting for side projectsShip the portfolio project that gets you hired — free credits for new accounts.Claim credits