retagged by
998 views
9 votes
9 votes

What will be the output of the following C program?

#include<stdio.h>
void main()
{
    int a =0;
    int b = (a-- ? a++: a ? a++ : a-- );
    printf("%d", b);
}
  1. $0$
  2. $-1$
  3. $-2$
  4. $2$
retagged by

1 Answer

14 votes
14 votes

a = 0

b = a– –  ? a++ : a ? a++ : a– –  $\equiv$  b = a– –  ? a++ : (a ? a++ : a– –), ternary operator is Right to Left associative.

It is of the form expr1 ? expr2 : expr3. Here, expr1 = a– –, expr1 returns 0 (False) and makes a = –1. Thus, we execute expr3. Here, expr3 = a ? a++ : a– –, a is –1 (True), so it executes a++, which returns –1 and makes a = 0.

Therefore, b = –1.

Answer :- B

 

Answer:

Related questions

9 votes
9 votes
3 answers
1