Ternary Operator in C Language

This operator also known as conditional operator . It is similar to if-else statement.

Syntax of ? operator

Expression1 ? Expression2 : Expression 3;

There are three expressions in ? operator, Expression1, Expression2, and Expression3 .

The value of a ? expression is determined like this −

  • If Exp1 is true, then Exp2 is evaluated.
  • If Exp1 is false, then Exp3 is evaluated.

Example: write a program to find the grater between two number using ? operator.

#include	
int main() 	
{	
    int a,b,c;
    a=10;
    b=20;
   	c =  (a>b) ? a : b ;
   	printf("\ngrater value = %d", c);
}

OUTPUT

grater value = 20