+ 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
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 !
+ 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
+ 2
Didn't I just answer the same question around 30 minutes ago? Why did you delete that one?
+ 1
Arsenic thanks
+ 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.
+ 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.
0
Thirt13n thanks I will try
0
XXX it was in c#
0
Arsenic ohh , then I should avoid gets()...
Again really thank you so much 💝🥰🥰
0
Arsenic bro i don't know how to use fgets() in c
can you tell me example code