To tell you the truth. In real code in production almost everyone uses something created to read because everything that exists has problems. This runs with several C functions, and this is one of the problems of a language that wants to provide only the basic and does not want to evolve (although this has its advantages too).
For exercises and simple codes depends on what you want. But I can already say that they thing to clean the buffer does not make sense and who knows how to program in real C knows it. There is a myth of using fflush(stdin)
. It even works on some compiler (because they chose to put it in their default library), but this is not standard C, so you should not use it. Unless you’re programming in a dialect of C. But if you’re learning, learn it right.
The scanf()
in general is not a good option except for the basic good. There are techniques that help in some problems, but can not control all situations. Can use for quick exercise.
The gets_s()
is much more simplistic and is only available in C11 compilers that it is medium rare to have implemented. If you use it gets kind of powerless to port.
Don’t use either, use fgets()
. This function is designed for more complex readings, but even it is not suitable for everything and may have problems with buffer. And she’s kind of boring to use, she has to understand how the data comes, eventually manipulate it, but programming in C and not understanding in depth what she’s doing doesn’t work.
At least you didn’t think about gets()
which is unsafe, already an advantage.
See also How to read from stdin in C? where shows the options.
If you don’t fully commit to learning C it’s best not to try too hard. I’m in favor of learning C as a way to understand what’s going on, but I don’t think you need to learn every detail if you’re not working as a C programmer. If you’re just learning to program and not the C itself, it matters little what you use, because what matters are other things of language. If you will learn the language for everyday use then both are bad.
then there is no difference between gets_s and fgets? , only that f stops when it has n?
– Vitor Gonçalves
@Vitorgonçalves edited, see if it helped more.
– Maniero
yes thanks , so I’m asking , no use only without knowing the reason and may is using a better. Thanks for the reply!!.
– Vitor Gonçalves
I’m sorry to open the @Niero topic again , but I tried to know more and I found scanf("%[ n]s", variable); (can I use it without problems?) , I still need to use the scanf so as not to end up cluttering with college , but I’m already finding out about fgets
– Vitor Gonçalves
For exercises, most of the time, yes, and in exercises there is a certain control that allows its use.
– Maniero