Multiple choice

What would be the output of the following?

void main() { int a=10,b; b=a++ + ++a; printf("%d %d %d %d",b,a++,a,++a); }

  1. 22 10 11 13

  2. 22 11 11 11

  3. 22 10 11 13

  4. 22 13 13 13

Reveal answer Fill a bubble to check yourself
D Correct answer
Explanation

In printf, arguments are evaluated right-to-left. Starting with a=10: a++ makes a=11 (but evaluates to 10 in first operation), then ++a makes a=12 (evaluates to 12). So b=10+12=22. For printf: ++a makes a=13, then a is 13, then a++ evaluates to 13 (a becomes 14), and b is 22. Output: 22 13 13 13.