+ 1

why "gets" does not take any input after "scanf" in C language ?

Here is a demo code : int main() { int a; char str[100]; scanf("%d",&a); gets(str); printf("%d",a); puts(str); } output : 2 2

15th Jul 2019, 7:13 PM
Kunal Kumar Thakur
Kunal Kumar Thakur - avatar
4 Réponses
+ 4
you need to input it on the same line, 2 "my name", works fine
15th Jul 2019, 10:33 PM
✳AsterisK✳
✳AsterisK✳ - avatar
+ 1
It is becasue the gets function reacts on the end of line \n of the previous multi-input.Just add \n into scanf format string just like this: https://code.sololearn.com/cf0z68dV0fdg/?ref=app
17th Jul 2019, 5:16 AM
Highman
Highman - avatar
+ 1
Thank You Highman. The reference code you provided solved my problem.
17th Jul 2019, 5:31 AM
Kunal Kumar Thakur
Kunal Kumar Thakur - avatar
+ 1
Thank you *Asterisk*
17th Jul 2019, 5:31 AM
Kunal Kumar Thakur
Kunal Kumar Thakur - avatar