C conditional operator rules

Rules of Using conditional [ ?: ] Operators in C

1 . It’s not compulsory that the conditional operators should be used only in arithmetic statements.

int i=1 ;
(i == 1 ? printf( "Case 1" ): printf("Case 2"));

Output :

Case 1

2.Conditional operators may be nested

int largest, a=10, b=20, c=30 ;
largest = (a > b ? ( a > c ? 10: 30 ) : ( b > c ? 20: 30 ) );
printf("Largst Number : %d",largest);

Output :

Largst Number : 30

3. Lvalue Required Error

a > b ? temp = a : temp = b ;
  1. In absence of parentheses the compiler believes that b is being assigned to the result of the expression to the left of second =
  2. So in order to avoid this error , use it like this -
a > b ? temp = a : (temp = b);