Conditional / Ternary operator in C

Ternary operator is a shorthand operator for if else and can be used to replace short if else statements in single line. Ternary operator is also known as conditional operator, inline if, ternary if.

As the name specifies ternary operator consist of three operand

Syntax:

Basic syntax of ternary operator
(boolean expression) ? (true-part) : (false-part)
Ternary operator in C

Let’s see a basic example of ternary/conditional operator:
max = (a>b) ? a : b
The above expression returns the maximum value between a and b and stores in max. If the boolean expression is true i.e. if (a>b) then the expression will return a, else the will return b.

The above code is similar to
if(a>b) 
{
    max = a;
}
else
{
    max = b;
}

Note: In C any non-zero integer value represents true and zero represents false.

Advantages of using ternary operator


Sample Program:

C program to find maximum between two numbers using ternary operator.
#include <stdio.h>

int main()
{
    int num1, num2, max;
    printf("Enter two numbers: ");
    scanf("%d %d", &num1, &num2);
    
    max = (num1>num2) ? num1 : num2;
    
    printf("Max is %d", max);
    
    return 0;
} 
Ouptut:
Enter two numbers: 20
30
Max is 30


NESTED TERNARY OPERATOR

Nesting of ternary operator is often dis-regarded by most of the programmer’s and even I also never recommend it. Nesting of ternary operators will definitely reduce the code length in characters but will also cost a poor readable code and more chances of error. Hence you must avoid a nested ternary operator as much as you can.

Sample program of finding maximum between three numbers using a nested ternary operator
#include <stdio.h>

int main()
{
    int num1, num2, num3, max;

    printf("Enter three numbers: ");
    scanf("%d %d %d", &num1, &num2, &num3);

    max = ((num1>num2) ? ((num1>num3) ? num1 : num3) : ((num2>num3) ? num2 : num3));

    printf("Max is %d", max);

    return 0;
}

In above program you can see that the ternary operator may had reduced the code length but had also made our program less readable. Hence to ensure readability of code we must use proper formatting for the ternary operator.

PROPER FORMATTING OF TERNARY OPERATOR

A proper formatted code will enhance your code readability and will also decrease the probability of errors in your code. A ternary operator must be well formatted in order to make it more readable and clear.

Have a look to the poorly formatted ternary operator.
max = ((num1>num2) ? ((num1>num3) ? num1 : num3) : ((num2>num3) ? num2 : num3));

The above code can be formatted as
max = (num1>num2) ? 
          ((num1>num3) ? num1 : num3) : 
      (num2>num3) ? num2 : num3;

The above code demonstrates that how a simple formatted code has increased the readability of our program.

Note: Always try to use ternary operator when the condition is simple and small else use simple if else.


You may also like
Operators in C
If else statement in C
Control statements in C


Labels: ,