O'Reilly logo

C Programming: Visual Quickstart Guide by Marc Liyanage, Larry Ullman

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 = ( ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required