ProgrammingHardIBM · Deloitte pattern
What happens when this runs?
What happens when this runs?
int a = 5;
int b = a++ + a++ + a++;
printf("%d %d", a, b);- A. 8 18, deterministically
- B. Undefined behaviour: a is modified three times in one expression with no sequence points separating the increments
- C. 8 15, deterministically
- D. 15 8, deterministically
Correct answer: B. Undefined behaviour: a is modified three times in one expression with no sequence points separating the increments
a++ + a++ + a++ performs three separate increment side effects on the same object a within a single expression, with no sequence points between them (addition does not provide one) — this is undefined behaviour, regardless of what any particular compiler's evaluation order happens to compute.
More C Programming questions
- What happens when this runs? c struct Point { int x, y; }; struct Point p = malloc(sizeof(struct Point)); printf("%d", p->x);
- What happens when this runs? c char concat(const char a, const char b) { char result[100]; strcpy(result, a); strcat(result, b); return…
- What is printed? c int a = 5, b = 2, c = 3; int result = a > b > c; printf("%d", result);
- What is printed? c int a = 1, b = 2, c = 3; int result = a == 1 && b == 2 || c == 100; printf("%d", result);
- What is printed? c int a = 4; int b = ~a + 1; printf("%d %d", b, -a);
- What is printed? c int x = 1, y = 2, z = 3; int result = x < y < z; printf("%d", result);