ProgrammingHardWipro · Cognizant pattern
What is printed?
What is printed?
int a = 3;
int result = a++ * 2 + --a;
printf("%d %d", a, result);- A. 4 9
- B. 3 8
- C. 3 9
- D. Undefined behaviour: a is both incremented and decremented within the same expression without a defining sequence between those two side effects relative to their uses
Correct answer: D. Undefined behaviour: a is both incremented and decremented within the same expression without a defining sequence between those two side effects relative to their uses
a++ (post-increment) and --a (pre-decrement) both modify `a` within the same full expression, and the + operator doesn't sequence its two operands relative to each other; two unsequenced side effects on the same object is undefined behaviour, so no single 'correct' numeric trace exists for this program.
More C Programming questions
- What is printed? c // header.h (included by both files) extern int sharedValue; c // a.c #include "header.h" int sharedValue = 42; c //…
- What is printed? c int a = 5, b = 2, c; c = a > b ? a++ : --b; printf("%d %d %d", a, b, c);
- What is printed? c int a = 2; int b = (a++, a + 3, a 2); printf("%d %d", a, b);
- What is printed? c int a = 10; int b = a++ + a++; printf("%d", a);
- What is printed? c int x = 5; if (x = 10) printf("true %d", x); else printf("false %d", x);
- What is printed? c int a = 1, b = 0, c = 1; int result = a || b && c; printf("%d", result);