Skip to main content

Final values of a, b and c

Short answer

a=11, b=25, c=260. a=(b++)+(++b)+a gives 10; c=10; b=(a++)+(b--)+a gives 26; c=c++*b-- gives 260 and b becomes 25. Note: multiple side effects on same variable yield technically undefined behavior.

Find out final value of a, b and c where following expressions are executed sequentially.

int a = 2, b = 3, c;
a = (b++) + (++b) + a;
c = a > b ? a:b;
b = (a++) + (b--) +a;
c = c++*b--;

Initially a = 2, b = 3.

Statement 1: a = (b++) + (++b) + a;

  • b++ gives 3 (b becomes 4); ++b makes b = 5 and gives 5.
  • a = 3 + 5 + 2 = 10, b = 5.

Statement 2: c = a > b ? a : b; Since 10 > 5, c = 10.

Statement 3: b = (a++) + (b--) + a;

  • a++ gives 10 (a becomes 11); b-- gives 5 (b becomes 4); the last a is 11.
  • b = 10 + 5 + 11 = 26.

Statement 4: c = c++ * b--;

  • c++ gives 10 and b-- gives 26, so c = 10 * 26 = 260 (the assignment overwrites the increment of c), and b-- makes b = 25.

Values after each statement:

After statementabc
1105not set
210510
3112610
41125260

Final values: a = 11, b = 25, c = 260.

Note: expressions such as (b++) + (++b) and c = c++ * b-- change the same variable twice without a sequence point, so the C standard leaves the result undefined; compilers warn about it and may give different values. The values above are the usual textbook evaluation, from left to right.

✓ Answer checked by Claude Opus 5.5 (high effort)

All Computer Programming chapter 3 questions →