Conditional Operator in C

Up until now, we have learned how to write logical programs where the decisions are made based on a specific condition. In this tutorial, we will learn a more concise way to make decisions that saves us a few lines of code.


The Conditional Operator

The C programming language has an operator that’s different from all the others we’ve learned so far. It’s called the conditional operator. It’s different from the other operators because it’s neither a unary nor a binary operator. It’s a ternary operator, which means it needs three operands to perform an operation. This operator allows an expression to produce one of two values depending on the value of the condition. The operator has two symbols (?:) and the syntax of the expression with this operator looks like the following:

expr1 ? expr2 : expr3

A conditional expression is any expression that uses the conditional operator (?:). Therefore, the expression expr1 ? expr2 : expr3 is a conditional expression.

A conditional expression contains three expressions: expr1, expr2, and expr3. If expr1 evaluates to a non-zero value, then the value of the entire conditional expression will be expr2. If expr1 evaluates to zero, then the value of the entire conditional expression will be expr3. We can use the result of a conditional expression however we want. For example, we could store the result in a variable like this:

result = expr1 ? expr2 : expr3

The above statement is equivalent to the following if-else statement:

if (expr1) {
    result = expr2;
else {
    result = expr3;
}

Keep this in mind: A conditional expression is an expression that produces a value that we can use in our program just like any other value.

It looks like the conditional operator provides us the way to write an if-else statement in a more concise manner. It reduces the lines of code. But, still it is not recommended to use it more often as it may reduce the readability of the code. The question that comes naturally is – in what situations, the conditional operator is preferred over the if-else statement?


When to Use the Conditional Operator?

Although it depends on the programmer when to use the conditional operator over the if-else statement, there are situations where most programmers are tempted to use the conditional operator to reduce the number of lines in their code.

For example, while writing a function, it may be possible that the return value of the function depends upon a specific condition like the following:

if(x > 0)
    return 1;
else 
    return 0;

The above code can be replaced by the following more concise code:

return x > 0 ? 1 : 0

Or may be, the following code is used in the program to print an appropriate message based on the specific condition:

if (x > 0)
    printf("Positive\n");
else
    printf("Negative\n");

The above code can easily be replaced by the following code:

printf(x > 0 ? "Positive\n" : "Non-positive\n");

In these situations, it’s a good idea (but not required) to use the conditional operator instead of the if-else statement. However, for complex if-else statements, it’s not a good idea to do so.



Leave a comment

Leave a comment

Your email address will not be published. Required fields are marked *

Thank you for choosing to leave a comment. Please be aware that all comments are moderated in accordance with our policy. For more information, please review our comments policy. Rest assured that your email address will not be shared with anyone. Kindly refrain from using keywords in your comment. Let’s maintain a respectful atmosphere and engage in meaningful conversations.