While using scanf it works fine but when I use fgets instead of scanf it only returns 1. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

While using scanf it works fine but when I use fgets instead of scanf it only returns 1.

https://code.sololearn.com/cmpFHf3HhZwU/?ref=app

26th Jul 2021, 12:06 PM
Kishan Khanna
Kishan Khanna - avatar
7 Answers
+ 2
Line 16: Insert a space after the %d specifier to prevent the line break character '\n' after the input number from getting left behind in the input stream. It's that '\n' that is read by fgets(), and it's also the reason why you got 1 as length of input. scanf( "%d ", &n ); Also, since you define <str> size was <n> characters, I would recommend to use <n> as the second argument to fgets() call at line 23, to be safe. So fgets() reads in accordingly to <str> size. fgets( str, n, stdin );
26th Jul 2021, 1:06 PM
Ipang
+ 1
Your question is not clear rewrite again with more discription
26th Jul 2021, 12:19 PM
A S Raghuvanshi
A S Raghuvanshi - avatar
+ 1
You did a lot of extra. I also had issues when '\n' character was in the input. Either way, see this https://code.sololearn.com/c20lsB5Q6gp3/?ref=app
26th Jul 2021, 12:43 PM
Slick
Slick - avatar
+ 1
Thanks everyone I have solved😊
26th Jul 2021, 1:14 PM
Kishan Khanna
Kishan Khanna - avatar
0
I mean fgets instead of scanf, bcz of buffer overflow.
26th Jul 2021, 12:30 PM
Kishan Khanna
Kishan Khanna - avatar
0
Yes, i did
28th Jul 2021, 1:17 PM
Kishan Khanna
Kishan Khanna - avatar
0
And thanks for this detailed explanation
28th Jul 2021, 1:18 PM
Kishan Khanna
Kishan Khanna - avatar