The Conditional Operator

? : takes three operands, each of which is an expression. They are arranged this way: expression1 ? expression2 : expression3. The value of the whole expression equals the value of expression2 if expression1 is true, and equals the value of expression3 otherwise.

Examples

( 5 > 3 ) ? 1 : 2 has the value 1.

( 3 > 5 ) ? 1 : 2 has the value 2.

( a > b ) ? a : b has the value of the larger of a or b.

Get C Primer Plus®, Third Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.