What is the best to use, scanf or get_s? in c

3

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

scanf() will just read Maria, get_s() will read it all, correct?

I am in doubt about the best one to use, my college professor says that we should clean the buffer for scanf() read, after all in C what better option to read strings with space ranges?

    
asked by anonymous 28.08.2018 / 21:16

2 answers

3

To tell you the truth. In actual 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 basics and does not want to evolve (although this has its advantages as well).

For exercises and simple codes it depends on what you want. But I can already tell you that cleaning the buffer does not make sense and who knows how to program in C actually knows this. 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 it should not use. Unless it's programming in a C. dialect. But if it's learning, learn right.

scanf() is not usually a good option , except for the basic good. There are techniques that help with some problems, but you can not control all situations. You can use it for quick exercise.

gets_s() is much simpler and is only available in C11 compilers which is a bit rare to have implemented. If you use it, you will not be able to carry it.

Do not use either, use fgets() . This function is designed for more complex readings, but even it is not good at all and may have buffer problem. And it is a little chatinha to use, has to understand how data comes, eventually manipulate it, but programming in C and not understanding in depth what you are doing does not work.

At least you did not think about gets() that is insecure, it's already an advantage.

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

If you do not commit yourself fully to learning C, you should not try hard. I am in favor of learning C as a way of understanding what is happening, but I do not think you need to learn every detail if you do not work as a C programmer. If it is only to learn to program and not C itself, it matters little what to use, because what matters is other things of language. If you are going to learn the language for day-to-day use then both are bad.

    
28.08.2018 / 21:29
0
char nome[25];
scanf(" %24[^\n]s", nome);

You can do this, the most important thing is to limit the scanf as it is in this example, if you remove the 24 and the user enter a name greater than 24 characters the program will be bugged.

In addition, scanf in this way will clear the 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 these 2, so I do not see great advantages, it's important to use scanf correctly.

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 person's name because it is necessary to clear buffer after reading int , in this case the most correct would be to use scanf the way I did. / p>

  • If you do not need to wipe buffer first then you can use fgets or gets_s , if you need to clear buffer then it is easiest to use scanf since that espaço is enough before % that solves the problem.
28.08.2018 / 21:27