Thursday 30 May 2019

Ternary Operator in C

Ternary Operator in C

Today we discuss about Ternary Operator in C. The ternary operator is an operator that takes three arguments. The first argument is a comparison argument, the second is the result upon a true comparison, and the third is the result upon a false comparison. Ternary operator is shortened way of writing an if-else statement.

Ternary Operator in C

Ternary operator is also said to be conditional operator. Ternary operator is a?b:c it say that the condition a is true b will be executed else C will be executed.

Example for Ternary Operator in C

#include<stdio.h>
int main()
{
int a=2,b=4,c=9;
int lar;
lar=(((a>b)&&(a>c))?a:((b>c)?b:c));
printf(“Largest Number is %d”,lar);
return 0;
}

Output: Largest Number is 9

1 comment: