ProgrammingHardIBM · Deloitte pattern
What is printed?
What is printed?
int x = 5;
int y = x++ + (x = 10);
printf("%d", y);- A. 15, deterministically
- B. 20, deterministically
- C. Undefined behaviour: x is modified by both x++ and the assignment within the same expression, unsequenced relative to each other
- D. 10
Correct answer: C. Undefined behaviour: x is modified by both x++ and the assignment within the same expression, unsequenced relative to each other
Both x++ (a side effect) and (x = 10) (another side effect) modify the same object x within one full expression, with the addition operator providing no sequencing guarantee between evaluating its two operands. Two unsequenced writes to the same scalar object is undefined behaviour in C, regardless of what any given compiler prints.
More C Programming questions
- Assume a 64-bit LP64 ABI. What is printed? c int a = 5, b = 10, c = 15; int arr[3]; arr[0] = &c; arr[1] = &a; arr[2] = &b; int sum = 0…
- Assume a 64-bit LP64 ABI. What is printed? c int a = 100; int p = &a; int q = p; p = NULL; printf("%d %d", (p == NULL), (q));
- Assume a 64-bit LP64 ABI. What is printed? c struct Point { int x, y; }; void movePoint(struct Point p, int dx, int dy) { p->x += dx; p->y…
- Assume a 64-bit LP64 ABI. What happens? c int createArray(int n) { int arr[n]; for (int i = 0; i < n; i++) arr[i] = i i; return arr; } int…
- Assume a 64-bit LP64 ABI. What is printed? c int a[3][3] = {{1,2,3},{4,5,6},{7,8,9}}; int sum = 0; for (int i = 0; i < 3; i++) sum +=…
- Assume a 64-bit LP64 ABI. What is printed? c int x = 10; int p = &x; p = p p; printf("%d", x);