+ 1

Why is gets() not working|| what is mistake in my code

I am trying different types of input and output in c But getchar() and scanf worked But gets() does not work... What is mistake in my code https://code.sololearn.com/cU5c8NoeaBLA/?ref=app

11th May 2021, 6:08 AM
Suraj Airi
13 Answers
+ 3
hey Suraj Airi when scan takes input from scanf() then it will not consume your space or newline character , so it is used by gets() ! simple solution is to put one extra getchar() after scanf() ! HOPE THIS HELP YOU BETTER !
11th May 2021, 6:15 AM
Giriraj Yalpalwar
Giriraj Yalpalwar - avatar
+ 3
gets() has been deprecated from the standards because of it dangerous behaviour of overflowing buffer. You can also see your compiler telling you the same in form of a warning. If you are looking for an alternative then you can use fgets() instead
11th May 2021, 6:15 AM
Arsenic
Arsenic - avatar
+ 2
Didn't I just answer the same question around 30 minutes ago? Why did you delete that one?
11th May 2021, 6:29 AM
XXX
XXX - avatar
+ 1
Arsenic thanks
11th May 2021, 6:18 AM
Suraj Airi
+ 1
Suraj Airi Nope. The code was exactly the same. The question was the same. I wrote almost the same thing Arsenic has said, and gave links to the fgets() and scanf() functions. Never mind though, I was just curious why you deleted that one.
11th May 2021, 6:33 AM
XXX
XXX - avatar
+ 1
XXX it is better to use getchar () After scanf with gets() as Thirt13n said
11th May 2021, 6:35 AM
Suraj Airi
+ 1
Suraj Airi nope it isn't, and even compier is warning you about the same. gets() only knows from where to start writting in your memory and don't know where to end as soon as user enters input of size greater than your buffer size ( 10 in your case ), your program will start showing undefined behaviour which can be anything from crashing your program with segmentation fault or worse, overwriting data in a memory location where it shouldn't, corrupting data which was already present there.
11th May 2021, 6:48 AM
Arsenic
Arsenic - avatar
0
Thirt13n thanks I will try
11th May 2021, 6:19 AM
Suraj Airi
0
XXX it was in c#
11th May 2021, 6:30 AM
Suraj Airi
0
Arsenic ohh , then I should avoid gets()... Again really thank you so much 💝🥰🥰
11th May 2021, 6:52 AM
Suraj Airi
0
Arsenic bro i don't know how to use fgets() in c can you tell me example code
11th May 2021, 7:09 AM
Suraj Airi