# The Ternary operator ( ? )

## The Ternary operator ( ? )

C contains a powerful and convenient operator that replaces certain statements of the if-then-else form.

### The general form of ternary operator ( ? ) is :

Expression 1 ? Expression 2: Expression 3 ;

The ? operator works like this: Expression 1 is evaluated. If it is true, Expression 2 is evaluated and becomes the value of the expression. If Expression 1 is false, Expression 3 is evaluated, and its value becomes the value of the expression.

Ex-
int   x, y  ;
scanf("%d ", & x) ;
y = ( x > 5 ? 3 : 4 ) ;

This statement will store 3 in y if x is greater than 5, otherwise it will store 4 in y.
The equivalent if statement will be,
if ( x > 5 )
y = 3 ;
else
y = 4 ;

### C Program to show the use of Ternary operator :

\* C Program to show the use of Ternary operator *\

# include < stdio.h >
int   main( )
{

int   x, y, dif  ;
printf(" Enter the first Number : ") ;
scanf("%d ", & x) ;
printf(" Enter the second Number : ") ;
scanf("%d ", & y) ;
dif = ( x > y ) ? ( x - y ) : ( y - x ) ;
printf("\n Difference of two number is : %d", dif) ;
return ( 0 );

}