The conditional operator (?) and (:) are sometimes called ternary operators since they take three arguments. In fact, they form a kind of foreshortened if-then-else. Their general form is,
Syntax:
expression 1 ? expression 2 : expression 3
What this expression says is: “if expression 1 is true (that is if its value is non-zero), then the value returned will be expression 2, otherwise the value returned will be expression 3”.
Example of Conditional Operator
Let us understand this with the help of an example:
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;