C | Input and Output | Question 13

Which of the following is true
(A) gets() can read a string with newline chacters but a normal scanf() with %s can not.
(B) gets() can read a string with spaces but a normal scanf() with %s can not.
(C) gets() can always replace scanf() without any additional code.
(D) None of the above


Answer: (B)

Explanation: gets() can read a string with spaces but a normal scanf() with %s can not. Consider following program as an example.

If we enter “Geeks Quiz” as input in below program, the program prints “Geeks”

filter_none

edit
close

play_arrow

link
brightness_4
code

int main()
{
   char str[100];
   scanf("%s", str);
   printf("%s", str);
}

chevron_right


But in the following program, if we enter “Geeks Quiz”, it prints “Geeks Quiz”

filter_none

edit
close

play_arrow

link
brightness_4
code

int main()
{
   char str[100];
   gets(str);
   printf("%s", str);
}

chevron_right



Quiz of this Question



My Personal Notes arrow_drop_up


Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.