Skip to main content

Ternary Operator in C

Ternary operator

The ternary operator is a shorthand way of writing an if-else statement in programming. It is more readable and concise than using if-else.

It has the following syntax:

(condition) ? (value if true) : (value if false)

The condition is evaluated first, if the condition is true, the operator returns the value following the ?, otherwise it returns the value following the :.

Using if-else vs Ternary Operator

Ternary operator is more concise and readable than if-else statement, making code more efficient and easier to maintain. It is useful when simple assignment based on a condition is needed.

Here's an example of program using an if-else statement:

int x = 10;
int y;
if (x > 5) {
y = 100;
} else {
y = 50;
}

Explanation

In this example, we are checking if the value of x is greater than 5. If it is, we set y to 100, otherwise we set it to 50.

The same program can be written using the ternary operator like this:

int x = 10;
int y = (x > 5) ? 100 : 50;

Explanation

This line of code is doing the same thing as the previous if-else block of code. The condition (x > 5) is evaluated first, if it's true, y will be set to 100, otherwise y will be set to 50.

when to use

In general, Ternary operator is useful when you want to assign value based on a condition and the both the values are known and simple.

When to avoid using Ternary operator

  • When the condition or the expressions are complex: If the condition or the expressions being evaluated are complex and hard to understand, it's better to use an if-else statement to make the code more readable.

  • When the code contains multiple statements: Ternary operator is best suited for simple assignments, when the code contains multiple statements, it can get confusing and hard to read.

  • When the code needs to handle multiple conditions: Ternary operator can handle only two conditions, if the code needs to handle multiple conditions, it's better to use if-elseif-else statements.

  • When the code needs to be debugged: Ternary operator can make debugging harder because the code is more compact and harder to understand.

For Example:

Here is a program in C where a ternary operator should not be used:

#include <stdio.h>

int main()
{
int x = 5, y = 10, z = 15;
int result;

/* Nesting multiple conditions in a ternary operator would make the code
* hard to read and understand */
result = (x > y) ? ((x > z) ? x : z) : ((y > z) ? y : z);

printf("Largest number: %d", result);

return 0;
}
Output:

Explanation

In this example, we are trying to find the largest number among three variables (x, y, and z) using a nested ternary operator. However, this makes the code hard to read and understand.

A better approach would be to use an if-else statement:

#include <stdio.h>

int main()
{
int x = 5, y = 10, z = 15;
int result;

/* Using an if-else statement would make the code more readable */
if (x > y && x > z)
{
result = x;
}
else if (y > z)
{
result = y;
}
else
{
result = z;
}

printf("Largest number: %d", result);

return 0;
}
Output:

Explanation

This way the code is more readable and easier to understand.