>I disagree. You have to have all kinds of special code to get scanf() to work properly. Only if you aren't using scanf for what it was designed in the first place. When you have to write workaround code to get a function to work, you're using the wrong tool or using it the wrong way.
My point exactly. scanf() is dangerous and inadequate reading strings (only reads to whitespace, and can easily blow your buffer if no spaces are in the input)
It's overkill if you want to read a single character. Why bring in the code for translating %d, %i, %f, etc. when getchar() does exactly the same thing in a small footprint?
After reading a number, the input stream still has characters left and must be cleared if reading strings next. This requires "fix-up" code.
I am of course targeting those that learn scanf() in school. Instructors never seem to explain any of the problems associated with the function, letting their students flounder. They are not taught the formatting characters required to use scanf() cleanly. That's why all the forums are inundated with questions about skipped input, partial input, and looping input.I'm not sure if he's being humble or really hasn't studied scanf fully. As some background, I, on the other hand, have studied it completely for many years, and also implemented it completely for both C89 and C99 several times (each in a different way). I like to think I have a strong understanding of how scanf works, and that's what guides my opinions. But that's probably also going to give me more of a liberal perspective because I'm not afraid of the function in any way as it holds no mysteries for me. :)Good for you. I on the other hand have found scanf() to be inadequate for production code because of it's lack of error checking, ability to bulletproof, and convoluted format specifiers attempting to correct anomalies. Therefore I use it only for testing.While he's not wrong (assuming it's a he), getchar has the same problem. This is related to how streams work, not scanf specifically.How manyWaltina's do you know :icon_rolleyes:
Yes, getchar() has the same problem (but it's less hidden IMO). My complaint is the function footprint in this case. I see a lot of code that uses only one scanf() , and that's to read a single character.Sure, scanf does a lot. But so does printf (probably even more so), and I don't see this guy railing on printf for taking up instruction space. ;) This whole part of the argument strikes me as unnecessary fluff that gets in the way of the real issues and tries to bolster the argument.
Formatted output is necessary, therefore so is printf() . Yes, formatted input is necessary, but scanf() has too many problems. sscanf() simply clears up the biggest problems. But using fgets() first allows the input to be checked before translating bad data. Can't do that with scanf() .Yea, if you have to clear the "keyboard" buffer like that, something is wrong with your input process. It's not scanf or getchar that left garbage in the stream, but the programmer's sloppy use of them.No it's not. It's the programmer's job to correct the sloppy function. The fact is that stuffis left in the input buffer. The programmer must understand that and "kludge" up his program to clear it. It's not a sloppy programmer...
The difference is that scanf can be fixed while gets can't. If you aren't providing a minimum field width for %s, you shouldn't be using it in the first place. Lack of understanding about the specifiers doesn't make scanf bad.
That's my complaint. Most instructors seem to never teach enough, letting the student's hang themselves.It's necessary after every input function at all to check the return status to see if it worked. That's programming 101, not an issue with scanf specifically.Yeah, but our friend does not return an error status. It simply returns the number of specifiers translated. If you are reading a%d field and enter 23R you get no indication of an error. It works fine. I suggest that mistyping should cause an error if an illegal character is seen.
You might say "Sure, but you're using fgets wrong if you want to check for long lines!", and I'd retort with "And you're using scanf wrong if you want to error check poorly formatted user input". scanf was designed for formatted input. User input can't be guaranteed to be formatted in the console. This is a case of the wrong tool being used for the job.
There's just so much wrong with what you just said.... I'l comment on this at the end.This is orders of magnitude more complicated, more error prone, and could easily result in the very code bloat that the author complains about for scanf. The strto* functions aren't exactly trivial, and you'd end up writing application specific parsing algorithms each and every time you want a number because any generic solution would cause the same problems that you're "solving" by not using sscanf.That's also why I don't use strto*() functions. Parsers are the most bulletproof functions available -- becauseyou write them to exacting specifications. And they can be added to a library for easy retrieval.
scanf is designed for production code, but it's not a panacea. This author seems to think that scanf is a jack of all trades meant to be used everywhere, when it really isn't.
What?!?! Where do you get the idea I think it is "meant to be used everywhere"? I thought I was clear --only during development because you control the input.
Despite what the regulars here say, switching from scanf to fgets/sscanf isn't going to solve all of your problems with scanf.
No, but it solves most of them...
------------You might say "Sure, but you're using fgets wrong if you want to check for long lines!", and I'd retort with "And you're using scanf wrong if you want to error check poorly formatted user input".
But scanf() is meant foruser input! Meatware sitting at a keyboard! Isn't that the definition of "poorly formatted input?" At least with fgets() you have the chance to test the input before translation and actually doing error cleanupbefore it's too late.
scanf was designed for formatted input. User input can't be guaranteed to be formatted in the console. This is a case of the wrong tool being used for the job.
My point exactly!!!! Since scanf() is designed for user input,and user input cannot be guaranteed to be formatted, then scanf() is the wrong tool for the job! Doh!
Allow me to reiterate:
scanf was designed for formatted input.User input can't be guaranteed to be formatted in the console.This is a case of the wrong tool being used for the job.
You said it yourself.... :icon_wink: