r/cprogramming • u/pruebax11 • Apr 13 '26
Scanf
mi pregunta raida en porque casi nunca salen tutos en youtube que expliquen como usar scanf realmente como su valor de retorno o parametros de control, y no hablan del buffer que deja y como solucionarlo, a diferencia de solo decir "es mala practica usar scanf" y te aseguro que si preguntas el porque solo te diran que despues de un espacio no lee mas y lo deja en el buffer huerfano, y causa errores si no se prevee
0
Upvotes
3
u/EpochVanquisher Apr 13 '26
Because people who know how to use scanf do not want to use scanf.
It is better to read a full line of text and parse it afterwards. You can use fgets to read a line, and then you can parse it. This is easier, more flexible, more powerful, and less prone to bugs.
2
u/bobotheboinger Apr 13 '26
Because there are just so many issues with using it right. I always use some other input function.
2
u/babysealpoutine Apr 13 '26
Regarding your question, you are far better off reading the man pages for C functions than searching for random videos, which can vary in accuracy. The Linux man page for scanf contains both a Caveats section and a Bugs section that explain the problem with scanf.
2
u/flatfinger Apr 14 '26
C was designed in an era before many of today's popular tools were invented, and where the fastest way to perform many of the tasks such tools have been designed to perform was to throw together a quick little C program to accomplish what needed to be done. In that kind of context, it didn't matter how a program would handle invalid input, because the only person who would ever use it was an author who would only ever feed it correct input. The fact that it was impossible to use functions like scanf or gets in a way that wouldn't malfunction badly when given invalid input simply didn't matter.
The usage cases around which scanf and gets were designed really don't exist anymore. Most of the tasks that could be accomplished most readily by writing one-off programs could be done more conveniently in languages that have been invented within the last half century than in C. If C was for some reason the best language for a one-off task, scanf() or gets() might be perfectly fine functions to use within it, but such situations are so rare that it's not worth learning about functions that wouldn't be appropriate anywhere else.
1
u/WorriedTumbleweed289 Apr 13 '26
I hate scanf. I prefer getline, then sscanf. Too many issues with input going to the next line.
0
u/yel50 Apr 13 '26
whoever told you the problems are related to its documented behavior of stopping on spaces doesn't know what they're talking about.
the problem is it doesn't protect against overflows, so it's a common culprit for buffer overflow attacks. it also doesn't check that numbers fit in the target size. you tell it to parse a 32 bit int and the input number requires 64 bits. it doesn't handle that.
it shouldn't be used because it's a relic of the 70s back before people realized how badly things could go.