Making Decisions in C: The Conditional Operator

Perhaps the most unusual operator in the C language is one called the conditional opera- tor. Unlike all other operators in C—which are either unary or binary operators—the conditional operator is a ternary operator; that is, it takes three operands. The two sym- bols that are used to denote this operator are the question mark (?) and the colon (:). The first operand is placed before the ?, the second between the ? and the :, and the third after the :.

The general format of the conditional operator is

condition ? expression1 : expression2

where condition is an expression, usually a relational expression, that is evaluated first whenever the conditional operator is encountered. If the result of the evaluation of con- dition is TRUE  (that is, nonzero), then expression1 is evaluated and the result of the evaluation becomes the result of the operation. If condition evaluates FALSE (that is, zero), then expression2 is evaluated and its result becomes the result of the operation.

The conditional operator is most often used to assign one of two values to a variable depending upon some condition. For example, suppose you have an integer variable x and another integer variable s. If you want to assign –1 to s if x were less than zero, and the value of x2 to s otherwise, the following statement could be written:

s = ( x < 0 ) ? -1 : x * x;

The condition x < 0 is first tested when the preceding statement is executed.

Parentheses are generally placed around the condition expression to aid in the statement’s readability. This is usually not required because the precedence of the conditional opera- tor is very low—lower, in fact, than all other operators besides the assignment operators and the comma operator.

If the value of x is less than zero, the expression immediately following the ? is evalu- ated. This expression is simply the constant integer value –1, which is assigned to the variable s if x is less than zero.

If the value of x is not less than zero, the expression immediately following the : is evaluated and assigned to s. So if x is greater than or equal to zero, the value of x * x, or x2, is assigned to s.

As another example of the use of the conditional operator, the following statement assigns to the variable maxValue the maximum of a and b:

maxValue = ( a > b ) ? a : b;

If the expression that is used after the : (the “else” part) consists of another conditional operator, you can achieve the effects of an “else if ” clause. For example, the sign function that was implemented in Program 6.6 can be written in one program line using two conditional operators as follows:

sign = ( number < 0 ) ? -1 : (( number == 0 ) ? 0 : 1);

If number is less than zero, sign is assigned the value –1; else if number is equal to zero, sign is assigned the value 0; else it is assigned the value 1. The parentheses around the “else” part of the preceding expression are actually unnecessary. This is because the con- ditional operator associates from right to left, meaning that multiple uses of this operator in a single expression, such as in

e1 ? e2 : e3 ? e4 : e5

group from right to left and, therefore, are evaluated as

e1 ? e2 : ( e3 ? e4 : e5 )

It is not necessary that the conditional operator be used on the right-hand side of an assignment—it can be used in any situation in which an expression could be used. This means that you could display the sign of the variable number, without first assigning it to a variable, using a printf statement as shown:

printf (“Sign = %i\n”, ( number < 0 ) ? –1 : ( number == 0 ) ? 0 : 1);

The conditional operator is very handy when writing preprocessor macros in C. This is seen in detail in Chapter 13, “The Preprocessor.”

This concludes the discussions on making decisions. In Chapter 7, “Working with Arrays,” you get your first look at more sophisticated data types. The array is a powerful concept that will find its way into many programs that you will develop in C. Before moving on, test your understanding of the material covered in this chapter by complet- ing the following exercises.

Source: Kochan Stephen G. (2004), Programming in C: A Complete Introduction to the C Programming Language, Sams; Subsequent edition.