ProgrammingVery hardProduct company OA
What can printf legally print here?
What can `printf` legally print here?
int i = 0;
printf("%d %d %d", i++, i++, i++);- A. Always 0 1 2, in that order
- B. Undefined behavior because i is modified three times in one statement
- C. Always 2 1 0, in that order
- D. Some permutation of 0, 1 and 2 (the exact order of argument evaluation is unspecified), and i ends up as 3
Correct answer: D. Some permutation of 0, 1 and 2 (the exact order of argument evaluation is unspecified), and i ends up as 3
Each argument's own side effect is complete before the call (indeterminately sequenced), so there is no interleaving and no UB — but the standard leaves the order in which the three arguments are evaluated unspecified, so the printed triple is some permutation of 0, 1, 2 and i finishes at 3.
More C Programming questions
- What happens when this is compiled? c int compute(int x); static int compute(int x) { return x 2; } int main(void) { printf("%d"…
- What is printed? c int a[] = {10, 20, 30, 40, 50}; printf("%d", a[1, 3]);
- What can be said about this program? c int main(void) { int arr[5]; arr[5] = 10; printf("%d", arr[5]); }
- What is the only output consistent with the C standard? c int a[3] = {10, 20, 30}; int p = a; printf("%d %d", p++, p);
- 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);