5.1.4 · D3C Programming

Worked examples — Operators — arithmetic, relational, logical, bitwise, assignment, comma

3,351 words15 min readBack to topic

The scenario matrix

Each row is a "case class" this topic can throw at you. The last column names the example that covers it.

# Case class The tricky part Covered by
A / and % both operands positive truncation toward zero Ex 1
B / and % with a negative dividend sign follows dividend Ex 2
C negative divisor sign still follows dividend, not divisor Ex 2
D integer division by zero (degenerate) undefined behaviour — must not run Ex 3
E int / vs float / (type-driven) one double promotes everything Ex 3
E2 float division by zero gives ±Infinity/NaN, not UB Ex 3
F Bitwise & | ^ bit-by-bit which bits survive Ex 4
G Left shift & signed right shift arithmetic vs logical shift Ex 5
G2 shift UB: negative / by too-large count / odd negatives out-of-range and rounding pitfalls Ex 5
H Short-circuit &&/|| skipping side effects which code never runs Ex 6
I Precedence trap a & b == c == beats & Ex 7
J Assignment chain + compound += right-associativity, evaluate-once Ex 8
K Comma operator value & sequence point value is the last operand Ex 9
L Real-world word problem pick the right operator Ex 10

Cells A–L are all hit below. Ranges and prerequisites link out to Data Types and Type Promotion, Two's Complement Representation, Sequence Points and Undefined Behavior, and Bit Masking Techniques.


Ex 1 — Positive / and % (Cell A)


Ex 2 — Negative dividend AND negative divisor (Cells B, C)

The number line below shows why step 1 lands on -3 and not -4: the true value -3.5 sits between them, and the yellow arrow points toward zero (rightward), so it snaps to -3. That single choice of rounding direction is what forces -7 % 2 = -1.


Ex 3 — Division by zero (integer AND float) & float promotion (Cells D, E, E2)


Ex 4 — Bitwise & | ^ bit-by-bit (Cell F)

Read the diagram column by column: each of the four rows is one 8-bit number. In the AND (yellow) row a box is filled only where both the 44 and 26 boxes above it were filled; in the XOR (pink) row a box is filled only where the two disagree. Counting the filled place-values gives 8 and 54.


Ex 5 — Shifts: multiply/divide, signed right shift, and the UB pitfalls (Cells G, G2)


Ex 6 — Short-circuit skips a side effect (Cell H)


Ex 7 — Precedence trap a & b == c (Cell I)

The precedence diagram makes the trap visual: == sits on a higher rung than &, so C reaches down and evaluates b == c first, feeding the 1 it produces into the &. The parentheses in the fix pull a & b back up above ==.


Ex 8 — Assignment chain + compound += (Cell J)


Ex 9 — Comma operator value & sequence point (Cell K)


Ex 10 — Real-world word problem (Cell L)


Recall Quick self-test

-9 / 4 and -9 % 4 in C99? ::: -9/4 = -2 (truncate toward zero); -9 % 4 = -9 - (-2)*4 = -1. a & b == c with a=6,b=4,c=4 — value and why? ::: 0; == binds tighter than &, so it's 6 & (4==4) = 6 & 1 = 0. x = (1, 2, 3); — value of x? ::: 3; comma operator yields its last operand. 1 || (i = 5) — does i change? ::: No; || short-circuits after the true left side, so the assignment never runs. -3 >> 1 vs -3 / 2? ::: -3 >> 1 = -2 (arithmetic shift floors toward -inf); -3 / 2 = -1 (truncates toward zero) — they disagree for odd negatives. 10.0 / 0.0 in C — crash? ::: No; floating divide by zero gives +Infinity (and 0.0/0.0 gives NaN). Only integer /0 is undefined behaviour.