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++);
  1. A. Always 0 1 2, in that order
  2. B. Undefined behavior because i is modified three times in one statement
  3. C. Always 2 1 0, in that order
  4. 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

Exams that ask this topic