ProgrammingHardIBM · Deloitte pattern

What is printed?

What is printed?
int a = 2;
int b = (a++, a + 3, a * 2);
printf("%d %d", a, b);
  1. A. 3 6
  2. B. 2 10
  3. C. 3 10
  4. D. 2 6

Correct answer: A. 3 6

The comma operator evaluates each sub-expression left to right (with a sequence point after each one) and the WHOLE expression's value is just its LAST operand's value, discarding the others. a++ runs first (a becomes 3); a + 3 is computed (5) but discarded; finally a * 2 is computed using a's now-current value, 3*2=6, which becomes b. Output: 3 6.

More C Programming questions

Exams that ask this topic