Conditional Expressions in C

In the C language, the if statement allows you to make a choice between two actions based on the value of a condition.

C also provides an operator that allows an expression to yield a value between two possible ones based on the value of a condition. These expressions are called Conditional Expressions.

The Conditional Operator

In the C language, there is a single ternary operator—that is, an operator that accepts three parameters—called the conditional operator.

The conditional operator consists of two symbols, the question mark ? and the colon :, which must be used as follows:

expression_1 ? expression_2 : expression_3

The three expressions above can be any kind of expressions, and the resulting expression is called a conditional expression.

The behavior of the conditional operator is as follows:

  1. The expression expression_1 is evaluated;
  2. If its result is different from 0, thus true, expression_2 is evaluated and the result of the entire expression is the result of expression_2;
  3. If the result of expression_1 is 0, thus false, expression_3 is evaluated and the result of the entire expression is the result of expression_3.

To clarify this, here's an example:

int x, y, z;

x = 10;
y = 20;

z = (x > y) ? x : y; /* z now holds 20 */
z = ((x < y) ? x : y) + 20; /* z now holds 30 */

In the example above, we have three variables, x, y, and z. We assigned the value 10 to x and the value 20 to y.

Subsequently, we assigned to z the result of the expression (x > y) ? x : y. Since x is less than y, the expression x > y is false, and so z takes the value of y, which is 20.

In the next line, we assigned to z the result of the expression ((x < y) ? x : y) + 20. Since x is less than y, the expression in parentheses took the value of x, which is 10, and we then add 20, obtaining 30.

An important note concerns the precedence of the conditional operator relative to other operators. The conditional operator has the lowest precedence among all C operators, so it is always evaluated last. For this reason, in the second assignment to z we used parentheses to force the evaluation order.

If we had written z = (x < y) ? x : y + 20;, the result would have been different, since the + operator has higher precedence than the conditional operator. In that case, z would have taken the value of y + 20, which is 40.

To recap:

Definition

Conditional Expression

A conditional expression is an expression that uses the conditional operator ? : to evaluate a condition and return a value based on the result of the condition.

The syntax of the conditional expression is as follows:

expression_1 ? expression_2 : expression_3

The overall result of the conditional expression is expression_2 if expression_1 is true, otherwise it is expression_3.

The conditional operator has the lowest precedence among all C operators and is the only ternary operator in the language.

Uses of Conditional Expressions

Conditional expressions make the source code more compact, but they have the downside of making the code less readable.

There are cases, however, in which they can be useful, such as in variable assignments or in simple mathematical expressions.

For example, combining printf with the conditional operator can make code simpler. Suppose we want to print the maximum between two variables. Normally, we would write the code like this:

if (x > y) {
    printf("The maximum is %d\n", x);
} else {
    printf("The maximum is %d\n", y);
}

With the conditional operator, we can write the same code more concisely:

printf("The maximum is %d\n", (x > y) ? x : y);

In this case, the expression (x > y) ? x : y returns the maximum between x and y, which is then passed as an argument to the printf function.

Conditional expressions can also be useful in mathematical expressions. For instance, suppose we want to calculate the absolute value of a number. Normally, we would write the code like this:

if (x < 0) {
    x = -x;
}

With the conditional operator, we can write the same code more compactly:

x = (x < 0) ? -x : x;

Summary

Conditional expressions are a powerful construct in C that allows you to evaluate a condition and return a value based on the result of that condition. The conditional operator is the only ternary operator in C and has the lowest precedence among all operators in the language.

Conditional expressions can be useful for making code more compact and readable in certain situations, but it is important to use them sparingly to avoid compromising code readability.