What’s best to use, scanf() or get_s()?

Asked

Viewed 1,292 times

4

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

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

I’m in doubt of the best use, my college professor says we should clean the buffer to the scanf() read, after all in C which best option to read strings with intervals of spaces?

2 answers

5


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?

  • @Vitorgonçalves edited, see if it helped more.

  • yes thanks , so I’m asking , no use only without knowing the reason and may is using a better. Thanks for the reply!!.

  • 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

  • For exercises, most of the time, yes, and in exercises there is a certain control that allows its use.

0

char nome[25];
scanf(" %24[^\n]s", nome);

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

Besides scanf that way it will clear the buffer first with that space before the %.


Another way would be to use the 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 do not see great advantages, it is important to use the 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 you will not read the person’s name because you need 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 up buffer first then you can use fgets or gets_s, if you need to clean the buffer then it’s easier to use scanf because that’s enough espaço before the % that solves the problem.
  • So the best is to use gets_s ? , I don’t know if you still have the problem of me limiting in char a vector of 10 for example and type 11 , he still keeps throwing 11 for char? (as it was with gets)

  • With gets_s also limits the number of characters char *gets_s( char *str, rsize_t n ); I would use the fgets or the scanf the way I did, it’s important to use it that way

Browser other questions tagged

You are not signed in. Login or sign up in order to post.