ProgrammingVery hardProduct company OA
What is the only output consistent with the C standard?
What is the only output consistent with the C standard?
int a[3] = {10, 20, 30};
int *p = a;
printf("%d %d", *p++, *p);- A. 10 20, always
- B. Undefined behavior, full stop
- C. 20 10, always
- D. Unspecified: the two printf arguments may be evaluated in either order, so '10 20' or '10 10' are both conforming
Correct answer: D. Unspecified: the two printf arguments may be evaluated in either order, so '10 20' or '10 10' are both conforming
*p++ dereferences p then increments it (postfix ++ binds tighter than *). Since the order in which printf's arguments are evaluated is unspecified, if *p++ runs first you get 10 then 20; if the plain *p runs first (before p is advanced) you get 10 then 10. Both are standard-conforming.
More C Programming questions
- What is printed? c int x = 5; x = x++ + 1; printf("%d", x);
- What is printed? c int i, j; for (i = 0, j = 10; i < 3; i++, j--) ; printf("%d %d", i, j);
- What is printed? Assume 32-bit int, two's complement. c unsigned int a = 5, b = 9; a = a ^ b; b = a ^ b; a = a ^ b; printf("%u %u", a, b);
- What is printed? c int flag = 0, mask = 2; flag |= mask == 2; printf("%d", flag);
- What is printed? c int a = 1, b = 2, c = 3; int arr[3] = {&a, &b, &c}; int p = arr; for (int i = 0; i < 3; i++) { (p + i) = 10; }…
- What is printed? (undefined behavior aside, assume typical stack layout where locals of adjacent calls can overlap after return) c int…