The Ternary Operator

C has an alternative syntax for the if-else conditional, referred to as the ternary (or trinary) operator. The name stems from the fact that there are three parts to using the operator. The basic syntax of it is

(condition) ? true_result :

					false_result;

What is notable about this operator is that it returns one of two values based on a condition. This returned value is normally assigned to a variable or printed. For example, the following will return a value indicating whether a number is even or odd (it uses the modulus operator to see if there's a remainder when the number is divided by 2):

 char even_odd; even_odd = ( ...

Get C Programming: Visual Quickstart Guide 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.