Which one is better to use, scanf or get s? in c

With the name 'Maria da silva' as an example:

scanf() you're just going to read Mary, get_s() you're going to read all of her, right?

I'm in doubt of the best to use, my college professor says we should clear the buffer for the scanf() Read, After all in C What better option to read strings with space intervals?

Author: Maniero, 2018-08-28

2 answers

To tell the truth none. In real code in production almost everyone uses something created to read because everything that exists has problems. This runs with various C functions, and this is one of the problems of a language that wants to provide only the basics 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 C of you do know this. Is there a myth to use fflush(stdin). It even works on some compiler (because they chose to put this in their default library), but this is not standard C, so it shouldn't use. Unless you are programming in a dialect of C. But if you are learning, learn right.

O scanf() in general it 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 on C11 compilers which is kind of rare to have implemented. If you use it is half without being able to carry.

Do not use either, use fgets(). This function is designed for more complex readings, but even it doesn't fit everything and may have a problem with buffer. And she is kind of a mess to use, she has to understand how the data comes, eventually manipulate it, but Program in C and not understand in depth what it's not working.

At least did not think about gets() which is unsafe, it is already an advantage.

See also How to read from stdin in C? where shows the options.

If you do not fully commit to learning C it is better not to try too much. 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 it's not working as a C programmer. little to use, because what matters are other things of the language. If you are going to learn the language for everyday use then both are bad.

 3
Author: Maniero, 2020-08-03 18:46:03
char nome[25];
scanf(" %24[^\n]s", nome);

You can do this way, the most important thing is to limit the scanf as is in this example, if you remove the 24 and the user enters a name greater than 24 characters the program will give bug.

Plus scanf this way will clear buffer first with that space before %.


Another way would be to use fgets(nome, 24, stdin); because it also limits the number of characters to use.


The gets_s will do the same as those 2, so I don't see great advantages, it is important is to use the scanf in a correct way.


try this piece of code:

int main()
{
  int x;
  char nome[25];
  scanf("%d", &x);
  fgets(nome, 24, stdin);
  printf("%s", nome);
}

What will happen is that it will not read the name of the person, because you have to clear the buffer after reading a int, in this case the most correct would be to use the scanf the way I did.


  • If you don't need to clean buffer first then you can use fgets or gets_s, if you need to clean buffer then it's easier to use scanf for it is enough that espaço before % that solves the problem.
 0
Author: Fábio Morais, 2018-08-28 19:36:13