Conditional Operator in C

This C Tutorial explains the Conditional Operator in C with examples.

What is Conditional Operator in C?

The conditional operator, also known as the ternary operator, is a operator that takes three operands. The first operand is a boolean expression, and the second and third operands are values. If the boolean expression evaluates to true, then the second operand is returned, otherwise the third operand is returned.

Syntax of the conditional operator is:

    expression1 ? expression2 : expression3;
                (or)
    condition ? value1 : value2

Besides Operator Precedence and Associativity, It, like the logical operators, does exert some control on the order of evaluation of the entire expression. For example:

Example 1:

    (number >= 1000) ? a + b + c : d + e + f;

Here in the above example, if (number >= 1000) is TRUE then result of whole expression is expression2 which is (a + b + c) and expression3 (d + e + f) is not evaluated at all. But if (number >= 1000) evaluates to FALSE then result of the entire expression is expression3 which is (d + e + f) and expression2 isn’t evaluated at all.

advertisement
advertisement

Example 2:

Here is another example of how the ternary operator can be used:

#include <stdio.h>
void main()
{
    int k = 8, m = 7, result;
    result = k < m ? k++ : m + 4;
    printf("%d", result);
}

In this case, the condition is k < m, i.e 8 < 7 which results in false.
As a result, the answer is m + 4 = 7 + 4 = 11.

Note: Join free Sanfoundry classes at Telegram or Youtube

Output:

11

If-else statement:

Basically, conditional operator is same as if-else construct. We can rewrite the above conditional expression as:

    if (expression1)
        expression2;
    else
        expression3;

Advantages of conditional operator over if-else construct:

advertisement

Then what is the difference between conditional expression and if-else statement. Firstly, conditional expression is little bit more compact than if-else construct. For example:

    /* b written twice using if-else */
    if (ch == 'A')
        b = 5; 
    else		    
        b = ch + 5;
 
    /* b written once using conditional operator */
    b = (ch == 'A') ? 5 : ch + 5;

Consider another example:

    /* subscript written twice using if-else */
    if (a > 10)
        b[a + c / 5 * 10 - 2] = 100;
    else 		
        b[a + c / 5 * 10 - 2] = 1000;
 
    /* subscript written once using ternary operator */
    b[a + c / 5 * 10 - 2] = a > 10 ? 100 : 1000;

Notice in the above example, subscript is a complex exp. and there are chances of errors while typing it twice as in if-else. So, using the conditional statement, we can avoid such errors.

advertisement

Related Articles

Sanfoundry Global Education & Learning Series – 1000 C Tutorials.

If you wish to look at all C Tutorials, go to C Tutorials.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.