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

Discussion

Which of the following comments regarding the reading of a string, using scanf(with optional) and get is true?

  • A.Both can be used interchangeably
  • B.Scanf is delimited by end of line, while gets is not
  • C.Scanf is delimited by blank, while gets is not
  • D.All of above

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.

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

Post your comment

Please log in or register to post a comment.