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);
  1. A. 10 20, always
  2. B. Undefined behavior, full stop
  3. C. 20 10, always
  4. 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

Exams that ask this topic