Discussion
Which of the following comments regarding the reading of a string, using scanf(with optional) and get is true?
Answer: C
Correct answer: Scanf is delimited by blank, while gets is not
scanf with %s stops at the first whitespace, so it reads one word however much was typed. gets reads until a newline, so it takes the whole line including embedded spaces.
That difference makes them anything but interchangeable - reading a name with scanf("%s") silently captures only the first word. It is worth adding that gets cannot limit the input to the buffer size and was removed from the language in C11; fgets is the correct replacement.
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