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”
int main() { char str[100]; scanf("%s", str); printf("%s", str); } |
But in the following program, if we enter “Geeks Quiz”, it prints “Geeks Quiz”
int main() { char str[100]; gets(str); printf("%s", str); } |
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- C | Input and Output | Question 13
- C | Input and Output | Question 12
- C | Input and Output | Question 9
- C | Input and Output | Question 8
- C | Input and Output | Question 11
- C | Input and Output | Question 13
- C | Input and Output | Question 6
- C | Input and Output | Question 3
- C | Input and Output | Question 5
- C | Input and Output | Question 4
- C | Input and Output | Question 13
- C | Input and Output | Question 2
- Basic Input and Output in C
- Input-output system calls in C | Create, Open, Close, Read, Write
- Output of C Program | Set 29
- Output of C programs | Set 63
- Output of C programs | Set 64 (Pointers)
- Code valid in both C and C++ but produce different output
- How to change the output of printf() in main() ?
- Output of C programs | Set 30 (Switch Case)

