1. Home
  2. Computer Science MCQs
  3. C Programming
  4. What is printed by printf("%d", printf("abc")); in C?

Added 30 Nov -0001

What is printed by printf("%d", printf("abc")); in C?

  1. 3abc, because the inner call's value is buffered first
  2. abc, with the outer %d printing garbage
  3. abc3, because printf returns the characters written Correct answer
  4. abc0, because the inner call returns nothing

Correct answer

C. abc3, because printf returns the characters written

15 views

MCQSEXAM

Explanation

printf returns the number of characters it successfully wrote, so the inner call prints abc and evaluates to 3, which the outer call then prints as the decimal 3. The visible output is therefore abc3 with no newline. The evaluation order of the two calls is unspecified, but the value is fully computed before the outer call executes, so the count is well defined here.

Reference

Asked in: NTS, PPSC IT, Campus Interviews

Preparing for an exam?

Doubts & discussion

Not sure why the answer is C? 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