- 2070 Chaitra · 4 marks
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);++bmakes 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 lastais 11.- b = 10 + 5 + 11 = 26.
Statement 4: c = c++ * b--;
c++gives 10 andb--gives 26, so c = 10 * 26 = 260 (the assignment overwrites the increment of c), and b-- makes b = 25.
Values after each statement:
| After statement | a | b | c |
|---|---|---|---|
| 1 | 10 | 5 | not set |
| 2 | 10 | 5 | 10 |
| 3 | 11 | 26 | 10 |
| 4 | 11 | 25 | 260 |
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)
Related questions
- Swap two values using pointers · Computer Programming, chapter 9
- State and prove final value theorem · Applied Mathematics, chapter 2 · asked 5×
- z-transform of (1-e^{-at}); final value · Applied Mathematics, chapter 2 · asked 2×
- Final value theorem for Z-transform · Mathematics III, chapter 3
- Final value theorem of z-transform · Digital Control System, chapter 2