Everyone learns that || short-circuits — if the left side is true, the right side never runs. This question from our C Programming Quiz App tests the flip side people forget: when the left side is false, the right side must run. And here, the right side divides by zero.
The Quiz Question
int a = 0;
printf("%d", a || (1 / a));
What is printed by this code?
- 1
- Divide-by-zero crash
- 0
- Undefined behavior
The Correct Answer: Undefined Behavior
Walk it through: a is 0, so the left operand of || is false. Short-circuiting only skips the right side when the left is true — so 1 / a is evaluated, and 1 / 0 is integer division by zero: undefined behavior (C11 §6.5.5p5). From that point the standard makes no promise whatsoever about what the program does.
What real machines actually did is a perfect illustration. We ran it on gcc 13.3 (x86-64 Linux) and Apple clang 21 (ARM64 macOS) — and neither crashed:
$ gcc -Wall -Wextra sc.c && ./a.out 0 $ echo $? 0
Both printed 0 and exited normally. UndefinedBehaviorSanitizer, meanwhile, calls it what it is:
$ gcc -fsanitize=undefined sc.c && ./a.out sc.c:4:28: runtime error: division by zero
Why Each Wrong Answer Is Wrong
Why not 1?
The tempting trace: “1/0 is… something nonzero? So 0 || nonzero is 1.” This was actually the answer our quiz app originally keyed as correct — a bug we found while auditing the question bank and fixed in version 2.1. The flaw: 1 / 0 doesn’t have a value to be nonzero. Once UB occurs, there is no arithmetic left to reason about — and indeed both our test machines printed 0, not 1.
Why not “Divide-by-zero crash”?
A crash is permitted — integer division by zero does raise SIGFPE on x86 in many builds — but it is not guaranteed, and neither of our two real compilers produced one here. That’s the difference between “typical symptom” and “defined behavior”. If the question asked “what may happen?”, crash would be one right answer among many; as “what does it print?”, only UB covers it.
Why not 0?
This is the sneakiest one: 0 is what we actually observed on both machines! But observing an outcome doesn’t make it the answer — a different compiler, optimization level, or architecture is free to crash or print 42. Undefined behavior means the standard has left the building; today’s 0 is a coincidence of code generation, not a promise.
How Short-Circuiting Actually Works
The rules, precisely:
x || y: evaluatexfirst (sequence point). If nonzero, result is 1 and y is never evaluated. If zero, evaluatey; result is 1 if y is nonzero, else 0.x && y: mirror image —yis skipped only whenxis zero.
Used correctly, short-circuiting is a safety device — the guard runs first:
int a = 2;
printf("%d", a || (1 / 0 == 0)); /* prints 1 — the division never happens */
That’s from a verified run (both compilers warn -Wdiv-by-zero about the constant, then never execute it — a is nonzero, so || short-circuits). The same pattern protects pointer code every day: if (p == NULL || p->len == 0). The quiz question is that idiom inverted: the guard is false, so the dangerous side runs. Guard with the condition that makes the danger unreachable — here, a != 0 && (1 / a) would have been the safe shape.
Frequently Asked Questions
Does || always skip the right operand in C?
No — only when the left operand is nonzero (true). When the left is 0, the right side must be evaluated to determine the result. 0 || anything always evaluates the right side.
Is integer division by zero undefined behavior in C?
Yes — for integers, x / 0 and x % 0 are undefined behavior. It may crash (SIGFPE on x86), return garbage, or appear to work; our gcc and clang test runs both quietly printed 0. (IEEE floating-point division by zero is different — it yields infinity or NaN.)
How do I catch division by zero bugs?
Compile with -fsanitize=undefined and run — UBSan reports the exact line (“runtime error: division by zero”). For divisions by a literal zero, gcc and clang also warn at compile time with -Wdiv-by-zero.
Related Reading
- Logical OR in C – Why 5 || 0 Prints 1
- Sequence Points in C – Why x++ + ++x Is Undefined
- 1 << 31 – Signed Overflow UB in C
- C Aptitude Questions and Answers
Recommended Books
- The C Programming Language – Kernighan & Ritchie (India) | Amazon.com
- C Programming: A Modern Approach – K.N. King (India) | Amazon.com
This question is #127 in the C Programming Quiz App — 155 questions with explanations covering operators, pointers, memory, and more.
Download on Google Play →