Skip to content
Tutorials Center
My learning

Topics

C Reference ยท Operators and Expressions

The conditional operator ?: in C

Reference for the C conditional operator ?: (ternary): syntax, type of the result, evaluation of only one branch, nesting, lvalue rules and common mistakes.

The conditional operator ?: is C's only ternary operator. In condition ? a : b the condition is evaluated first. If it is non-zero the expression has the value of a; otherwise it has the value of b. Only the chosen branch is evaluated.

Because it is an expression, not a statement, it can sit inside an initializer, a function argument, a return or a macro, where an if cannot. Use it for short choices of a value; use if when each branch does real work.

C
int larger = (a > b) ? a : b;
printf("%s\n", count == 1 ? "item" : "items");

Try it

C Choosing a value

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

C Only one branch runs, and the result type

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

Syntax

C
condition ? expression-if-true : expression-if-false
Part Description
condition A scalar expression: arithmetic or pointer. Non-zero is true. A sequence point follows it.
expression-if-true Evaluated, and used as the result, only if the condition is true.
expression-if-false Evaluated, and used as the result, only if the condition is false.

Result type: if both branches are arithmetic, the usual arithmetic conversions give a common type; two pointers must be compatible (or one is void * or a null pointer constant); two structs must have the same type; both void gives a void expression. The result is not an lvalue in C.

Errors / undefined behavior: none of its own. Unsequenced side effects in the branches that touch the same object as the other operand remain undefined.

Usage notes

  • Associativity is right to left, so a ? b : c ? d : e means a ? b : (c ? d : e). That makes if-else-if chains readable when formatted as in the first example, but deep nesting becomes hard to read; prefer a switch or a lookup table.
  • Precedence is low, just above assignment. Put the whole conditional in parentheses when you combine it with other operators: a + b ? c : d means (a + b) ? c : d, and 1 + x ? 2 : 3 is not 1 + (x ? 2 : 3).
  • The branches convert to one type. cond ? 1 : 2.5 is always a double, even when the integer branch is chosen. Mixing signed and unsigned operands follows the usual arithmetic conversions, so -1 can become a huge value.
  • It is not an lvalue in C: (c ? a : b) = 5 is an error (valid in C++). Use pointers: *(c ? &a : &b) = 5;.
  • Only the chosen branch runs, which lets you guard work: p ? p->value : 0. Do not rely on side effects in the branches for program logic.
  • Strings: choosing between literals is fine (flag ? "yes" : "no"), and the result type is char * (use const char * to store it).
  • Macros: wrap the arguments and the whole expression in parentheses (#define MAX(a, b) ((a) > (b) ? (a) : (b))) and remember that each argument may be evaluated twice, see macro pitfalls.
  • GNU extension: a ?: b (omitting the middle operand) is available in GCC and Clang but is not standard C.