Machine-translated by Google. Questions are authored in English.
Top

Discussion

If abc is the input, then the following program fragment
char x, y, z ;
printf ("% d" , scanf ("%c%c%c" , &x , &y , &z )) ;
results in

  • A.a syntax error
  • B.a fatal error
  • C.segmentation violation
  • D.printing of 3

Answer: D

Correct answer: printing of 3

scanf returns the number of input items successfully assigned. With the input abc and three %c conversions, all three characters are read and assigned, so it returns 3, and the outer printf prints that.

Note that %c does not skip whitespace as most conversions do, so the three characters read are exactly the next three in the stream. Checking this return value is the correct way to detect a failed or partial scanf, and neglecting it is a common source of bugs.

No comment is present. Be the first to comment.
Loading…

Post your comment

Please log in or register to post a comment.