8.4.1 Rules for the Conditional Operator

The first operand, condition, should be a value that can be compared with zero—a number or a pointer. If it is true (nonzero), then the conditional expression computes iftrue and its value becomes the value of the conditional expression. Otherwise the conditional expression computes iffalse and its value becomes the value of the conditional expression. The conditional expression always computes just one of iftrue and iffalse, never both of them.

Here’s an example: the absolute value of a number x can be written as (x >= 0 ? x : -x).

Warning: The conditional expression has rather low syntactic precedence. Except when the conditional expression is used as an argument in a function call, write parentheses around it. For clarity, always write parentheses around it if it extends across more than one line.

Warning: Assignment operators and the comma operator (see Comma Operator) have lower precedence than conditional expressions, so write parentheses around those when they appear inside a conditional expression. See Order of Execution.

Warning: When nesting a conditional expression within another conditional expression, unless a pair of matching delimiters surrounds the inner conditional expression for some other reason, write parentheses around it:

((foo > 0 ? test1 : test2) ? (ifodd (foo) ? 5 : 10)
                           : (ifodd (whatever) ? 5 : 10));

In the first operand, those parentheses are necessary to prevent incorrect parsing. In the second and third operands, the computer may not need the parentheses, but they will help human beings.