1. Home
  2. Computer Science MCQs
  3. C Programming
  4. Both f() and g() are passed to printf as arguments and…

Added 30 Nov -0001

Both f() and g() are passed to printf as arguments and each increments the same global counter. What does standard C guarantee about the values printed?

  1. g() always runs first, because the arguments are evaluated right to left
  2. Nothing; the calls are indeterminately sequenced, so either order can occur Correct answer
  3. f() always runs first, since it appears leftmost in the argument list
  4. The program has undefined behaviour, because one global object is written twice

Correct answer

B. Nothing; the calls are indeterminately sequenced, so either order can occur

19 views

MCQSEXAM

Explanation

Function argument evaluation order is unspecified in C: the compiler may evaluate f() or g() first, and the two calls are indeterminately sequenced with respect to each other. Because the increments are not unsequenced side effects on the same scalar, the program is not undefined, only output-dependent, so no single printed order can be relied on. Contrast this with i = i++, where two unsequenced writes to one object are genuinely undefined behaviour.

Reference

Asked in: NTS, PPSC IT, Campus Interviews

Preparing for an exam?

Doubts & discussion

Not sure why the answer is B? Ask here and get an explanation.

Ask a doubt about this MCQ

Shown after review. No links please.

More C Programming MCQs

Practise all C Programming MCQs

Similar MCQs