Explanation: Ternary Operator in C

Explanation: Ternary Operator in C

The ternary operator in C is another name for the conditional operator. Conditional statements are decision-making statements that are based on the expression’s outcome. The symbols ‘?’ and ‘:’ are used to symbolize it.

The ternary operator is named by the fact that the conditional operator works with three operands.

Because the ‘if-else‘ statement is also a decision-making statement, the behavior of the conditional operator is similar to that of the ‘if-else‘ statement.

The ternary operator is a computer language operator that takes three operands instead of the usual one or that maximal operators employ. It demonstrates how to condense a simple if else block.

The ternary operator contains three arguments:

  1. The first one is a comparison operator.
  2. The second one is the result of a true comparison
  3. And the third one is the result of a false comparison

A ternary operator is a beneficial tool that you will come across in code quite often. Ternary operators could make statements extra concise and easy to purpose about.

int a = 10, b = 20, c;

if (a < b) {

    c = a;

}

else {

    c = b;

}

printf("%d", c);


In general, the above program takes more than ten lines but by using the ternary operator we can compile and run the program within three lines.

Syntax

condition ? value_if_true : value_if_false

The statement evaluates to value_if_true if the condition is matched, and value_if_false otherwise.

The above code is an example of using ternary operator.

int a = 10, b = 20, c;

c = (a < b) ? a : b;

printf("%d", c);


The correct output of the above program is 10

Here C and Ahold the same value because the condition a < b was true.

Remember that the arguments value_if_true and value_if_false should be of the equal type and that they must be easy expressions instead of complete statements.

The ternary operator is also known as the conditional operator uses three operands to perform the operation.

The ternary operation can be nested like if-else programs

int a = 1, b = 2, ans;

if (a == 1) {

    if (b == 2) {

        ans = 3;

    } else {

        ans = 5;

    }

} else {

    ans = 0;

}

printf ("%d\n", ans);

 

The above program is an easy example of if-else statements. The same program we can write by using a nested ternary operator.

int a = 1, b = 2, ans;

ans = (a == 1 ? (b == 2 ? 3 : 5) : 0);

printf ("%d\n", ans);


So the output should be 3 in both cases.

 

Although the behavior of a conditional operator and an ‘if-else statement is similar, there are several distinctions. Let’s have a look at the distinctions between them.

  • A conditional operator is a single programming statement, whereas an ‘if-else statement is a programming block with statements enclosed in brackets.
  • A conditional operator can be used to assign a value to a variable, although the ‘if-else’ statement cannot.
  • When there are many statements, it is not beneficial for executing them, however, the ‘if-else’ statement is more appropriate.
  • While the nested ternary operator is more complex and difficult to debug, the nested ‘if-else’ statement is simple to read and maintain.

The Difference Between If-Else Statement and Ternary Operator Using Code

C PROGRAM TO FIND THE LARGEST NUMBER BETWEEN TWO NUMBERS

The C programs below discover the biggest number between the two numbers. The largest number is found in the first program using if-else expressions, and the largest number is found in the second program using the ternary operator.

USING IF-ELSE STATEMENT:

#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:

Enter any two numbers

23

22

23 is the largest numbers of given numbers

 

Read More:- Python New Line: How to Print Without a Newline in Python

USING THE TERNARY OPERATOR FIND THE LARGEST NUMBER:

#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:

Enter any two numbers

45  33

45  is the largest number of given numbers

 

You may have noticed that the output of both codes is identical, but the first example employs if-else statements, whilst the second example used the ternary operator to accomplish the same task. The key difference between the two codes is that the second one is more concise than the first.

Back to top
%d bloggers like this: