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

In C, the real utility of it is that it’s an *expression* instead of a statement; that is, you can have it on the right-hand side (RHS) of a statement. So you can write certain things more concisely.

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

**Exp1 ? Exp2 : Exp3**

**OR**

**(condition) ? (if_true) : (if_false)**

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

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 2
^{nd}and the 3^{rd}operands are of same type, the result will also be of that type. - If 2
^{nd}and 3^{rd}operands are of enumeration or arithmetic types, then the arithmetic conversions will be performed to transform them to a common type. - If 2
^{nd}and 3^{rd}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 2
^{nd}and 3^{rd}both operands are of reference types then of course reference conversions will be performed to convert both operands to a common type. - If 2
^{nd}and 3^{rd}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 2^{nd} and 3^{rd} operands other than above mentioned list are illegal.

Let’s consider some example to get hands-on knowledge of ternary operator.

## C Program to find the largest number 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:**

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
#include<stdio.h> int main() { int a,b; printf("Enter any two numbers \n"); scanf("%d%d", &a , &b); if(a>b) { printf("%d",a); printf(" is largest number of given numbers \n"); } else { printf("%d",b); printf(" is largest number of given numbers \n"); } return 0; } |

**Output:**

1 2 3 4 |
Enter any two numbers 23 22 23 is the largest numbers of given numbers |

#### Using Ternary Operator:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
#include<stdio.h> int main() { int a, b, max; printf("Enter any two numbers \n"); scanf("%d%d", & a, & b); /* Following statement replaces the whole if-else statement and makes the code more concise*/ max = (a > b) ? a : b; printf("%d", max); printf("is the largest number of given numbers"); return 0; } |

**Output:**

1 2 3 4 5 |
Enter any two numbers 45 33 45 is the largest number of given numbers |

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,

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

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 m*ax* 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.

** ****Using Ternary Operator:**

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
#include<stdio.h> int main() { int a, b, c, max; printf("Enter any three numbers\n"); scanf("%d%d%d", &a, &b, &c); max= (a > b) ? (a > c ? a : c) : (b > c ? b : c); printf ("%d", max); printf (" is the largest number of given numbers"); return 0; } |

**Output:**

1 2 3 |
Enter any two numbers 4 5 6 6 is the largest number of given three numbers |

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

1 |
max = (a > b) ? (a > c ? a : c) : (b > c ? b : c); |

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.*

## Let’s consider another example to make your concepts more clear

1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
#include <stdio.h> int main() { int a, b, c, max; printf("Enter any three numbers\n"); scanf("%d%d%d", &a, &b, &c); (a > b)? (a > c ? printf(" the largest number is : %d" , a) : printf ("the largest number is : %d", c) : (b > c ? printf ("the largest number is: %d", b) : printf(" the largest number is : %d", c); return 0; } |

**Output:**

1 2 3 |
Enter any two numbers 7 8 9 the largest number is 9 |

I have repeated this example here, the purpose of repeating it again is to make it clear that you can directly writing the
printf()* *statements in place of 2^{nd} and 3^{rd} operands.

I hope this article will help you. Kindly let me know about your feedback in the comments section.