The ternary operator ? : is C’s only three-operand operator — a compact if/else that produces a value. The expression in this question from our C Programming Quiz App is the most famous ternary of all: the minimum idiom.
The Quiz Question
int a = 5, b = 10;
printf("%d", a < b ? a : b);
What is printed by this code?
- 5
- 10
- 1
- 0
The Correct Answer: 5
Read cond ? x : y as “if cond, then x, else y”. Here the condition a < b is true (5 < 10), so the expression evaluates to the middle operand — a, which is 5. It’s the classic minimum of two numbers one-liner. Verified on gcc 13.3 and Apple clang 21, warning-free:
$ gcc -Wall -Wextra ternary.c && ./a.out 5
Flip the comparison and you get the maximum — from the same verified run:
printf("%d\n", a < b ? a : b); /* 5 — min */
printf("%d\n", a > b ? a : b); /* 10 — max */
Why Each Wrong Answer Is Wrong
Why not 10?
10 is the else branch — chosen only when the condition is false. Picking 10 usually means reading the operands in the wrong order: the value right after ? is the “true” result, the one after : is the “false” result.
Why not 1?
1 is what would print if only the comparison a < b were passed to printf — comparisons in C yield 1 or 0, as our equality post shows. But the ternary consumes that 1 to select a branch; the 1 itself is never the result. Precedence backs this up: < binds tighter than ? :, so the expression groups as (a < b) ? a : b.
Why not 0?
0 appears nowhere: the condition is true (so not 0), and neither branch holds 0. Even if the condition were false, the result would be b = 10.
Two Properties Worth Knowing
Only one branch is evaluated. Like && and ||, the ternary is a control-flow point with a guaranteed sequence point after the condition: the not-chosen branch never runs. p ? p->len : 0 is safe on a NULL p for exactly this reason — same family of guarantees as short-circuit evaluation.
It’s an expression, not a statement. That’s its whole reason to exist — it goes where if/else can’t: inside an argument list (as in our quiz line), an initializer (int min = a < b ? a : b;), or a return. The flip side: nesting ternaries two or three deep produces famously unreadable code. Good rule — one level is idiomatic, two needs parentheses and a comment, three should be an if/else.
Frequently Asked Questions
How does the ternary operator work in C?
cond ? x : y evaluates cond; if nonzero the result is x, otherwise y. Only the selected branch is evaluated, and there’s a sequence point after the condition.
What does a < b ? a : b compute?
The minimum of a and b: when a is smaller the condition is true and a is chosen; otherwise b. Swap to a > b ? a : b for the maximum.
Does the ternary operator evaluate both branches?
No — exactly one. That makes guards like p ? p->value : default_value safe even when evaluating the other branch would crash.
Related Reading
- Equality Operator in C – Why 3 == 3 Prints 1
- Short-Circuit Evaluation in C
- Sequence Points 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 #105 in the C Programming Quiz App — 155 questions with explanations covering operators, pointers, memory, and more.
Download on Google Play →