In C Programming, ternary operator allows executing different code depending on the value of a condition. The returned value is the result of the expression when the code is executed. The main advantage of using ternary operator is to reduce the number of lines of code and improve the performance of application.

In C, the real utility of ternary operator is that it is an expression instead of a statement i.e. you can have it on the right-hand side (RHS) of a statement. So you can write certain code statements more concisely.

Table of Contents

The ternary operator uses three operands. It evaluates a condition i.e. Expression1 and then depending upon the result of condition, chooses one of its two branches (Expression2 or Expression3) for execution. The symbols used for ternary operator in C are “?” and “:” and syntax for the ternary operator is:

Expression1 ? Expression2 : Expression3

OR

(condition) ? (if_true) : (if_false)

In the above syntax, Expression1 is condition (a Boolean expression, which can either be true or false) while Expression2 and Expression3 can either be variable or numeric value or any mathematical expression or statement. If the condition would be true, Expression2 will be executed else Expression3 will be executed.

Ternary Operator Diagram
Ternary Operator Flow Diagram

Ternary operator can be used in place of if-else statements. Ternary operator is also known as conditional operator, which I guess is more meaningful, as it evaluates the conditions just like if. Furthermore, the ternary operator makes the code more concise without compromising readability.

Before getting to the examples of ternary operator, consider the following.

Rule for 1st operand:

All conditional expressions possess right-to-left associativity where the 1st operand of the expression must be of integer or pointer type.

Rules for 2nd and 3rd operands:

  • If both 2nd and the 3rd operands are of same type, the result will also be of that type.
  • If 2nd and 3rd operands are of enumeration or arithmetic types, then the arithmetic conversions will be performed to transform them to a common type.
  • If 2nd and 3rd both operands are of pointer type or if anyone one of them is of pointer type and the other one is constant expression, evaluates to 0, then simply pointer conversions will be performed to convert both of them to a common type.
  • If 2nd and 3rd both operands are of reference types then of course reference conversions will be performed to convert both operands to a common type.
  • If 2nd and 3rd both operands are of type void then the common type will be void.
  • If both operands have user-defined type, the common type is that type.
  • If both operands are of different types and out of them one has user-defined type, the language rules will be used to convert them to a common type.

Any combinations of 2nd and 3rd operands other than above mentioned list are illegal.

Let’s consider some example to get hands-on knowledge of ternary operator. Similarly, Ternary operator in C++ evaluates the conditions and executes the code block.

C Program to find the Maximum Between Two Numbers

The following C programs find a largest number from two numbers provided. First program uses if-else statements to determine the largest number and second program finds the largest number using ternary operator.

Using if-else Statement:

Output:

Using Ternary Operator Find the Largest Number:

Output:

Here, you may have observed that the output of both codes is same, but the first example uses if-else statements while second example did the same job using ternary operator. The main difference is that the second code is more concise as compared to first one.

Now,

The above mentioned statement is the real game changer. The condition is (a>b), that will compare the value of a and b that either the value of a is greater than b or not, value of a will become equivalent to whole expression and assigned to variable max if the expression is true. But if it is not the case, than b then the whole expression will become equivalent to value of b and its value will be assigned to variable max.

Now let’s try a bit complex example of ternary operator:

C Program to Find the Largest Number Among Three Numbers

The following C programs find a largest number from two numbers provided. First program uses if-else statements to determine the largest number and second program finds the largest number using ternary operator.

Output:

In above mentioned example, 3 numbers are compared using ternary operator.

In above statement, nested ternary operators have been used. (a>b) compares the value of a and, if the value of a is greater than value of b, (a>c?a:c) will be executed that further compares the value of a with the value of c, if the value of a is greater than the value of c, the whole expression will become equivalent to the value of a and assigned to max. But if the value of a is smaller than the value of c the whole expression will become equivalent to the value of a and assigned to max.

And if (a>b) becomes false, then (b>c?b:c) will be executed that further compares the value of b with the value of c, if the value of b is greater than the value of c, the whole expression will become equivalent to the value of b and assigned to max. But if the value of b is smaller than the value of c the whole expression will become equivalent to the value of b and assigned to max.

Example C Program to Find the Odd or Even number using Ternary Operator

Output:

These examples showcase the basic usage of the ternary operator in different scenarios. It is important to note that while the ternary operator can make code more concise, it should be used judiciously to maintain code readability.