×
☰ Menu

 Conditional operators

A ternary operator pair "  ? :  " is available in C to construct conditional expression of the form 

 

exp1?exp2:exp3

where exp1, exp2, and exp3 are expressions.

  • exp1 is evaluated first, if it is true, then exp2 is evaluated and becomes the value of the expression.
  • if exp1 is true, false exp3 is evaluated and becomes the value of the expression.

 

 

Code: 1

#include <stdio.h>
main() 
{
int a=10,b=15,x;

x=(a>b)?  a : b;
	
printf("%d\n", x );
}

 

Code: 2

#include <stdio.h>
main() 
{
int a=10,b=15,x;


if (a>b)
	x=a;
else 
	x=b;	
	
printf("%d\n", x );
}

 

Output: 

15

--------------------------------
Process exited after 1.595 seconds with return value 0
Press any key to continue . . .