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
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.
Post your comment
Please log in or register to post a comment.
Examination Categories
- SSC Exams
- Banking Exams
- Teaching Exams
- Civil Services Exams
- Railways Exams
- Engineering Recruitment Exams
- Defence Exams
- State Government Exams
- Police Exams
- Insurance Exams
- Nursing Exams
- Other Government Exams
- NRA CET
- PG Entrance Exams
- Campus Placement
- MBA Entrance Exams
- CUET and UG Entrance
- Judiciary Exams
- Regulatory Body Exams
- Software Development