Home > OS >  How to definitely solve the scanf input stream problem
How to definitely solve the scanf input stream problem

Time:01-08

Suppose I want to run the following C snippet:

scanf("%d" , &some_variable);
printf("something something\n\n");

printf("Press [enter] to continue...")
getchar(); //placed to give the user some time to read the something something

This snippet will not pause! The problem is that the scanf will leave the "enter" (\n)character in the input stream1, messing up all that comes after it; in this context the getchar() will eat the \n and not wait for an actual new character.

Since I was told not to use fflush(stdin) (I don't really get why tho) the best solution I have been able to come up with is simply to redefine the scan function at the start of my code:


void nsis(int *pointer){ //nsis arconim of: no shenanigans integer scanf
     scanf("%d" , pointer);
     getchar(); //this will clean the inputstream every time the scan function is called
}

And then we simply use nsis in place of scanf. This should fly. However it seems like a really homebrew, put-together-with-duct-tape, solution. How do professional C developers handle this mess? Do they not use scanf at all? Do they simply accept to work with a dirty input stream? What is the standard here?

I wasn't able to find a definite answer on this anywhere! Every source I could find mentioned a different (and sketchy) solution...


EDIT: In response to all commenting some version of "just don't use scanf": ok, I can do that, but what is the purpose of scanf then? Is it simply an useless broken function that should never be used? Why is it in the libraries to begin with then?

This seems really absurd, especially considering all beginners are taught to use scanf...


[1]: The \n left behind is the one that the user typed when inputting the value of the variable some_variable, and not the one present into the printf.

CodePudding user response:

but what is the purpose of scanf then?

An excellent question.

Is it simply an useless broken function that should never be used?

It is almost useless. It is, arguably, quite broken. It should almost never be used.

Why is it in the libraries to begin with then?

My personal belief is that it was an experiment. It tries to be the opposite of printf. But that turned out not to be such a good idea in practice, and the function never got used very much, and pretty much fell out of favor, except for one particular use case...

This seems really absurd, especially considering all beginners are taught to use scanf...

You're absolutely right. It is really quite absurd.

There's a reason all beginners are taught to use scanf. During week 1 of your first C programming class, you might write the little program

#include <stdio.h>

int main()
{
    int size = 5;
    for(int i = 0; i < size; i  ) {
        for(int j = 0; j < size; j  )
            putchar('*');
        putchar('\n');
    }
}

to print a square. And during week 1, to make a square of a different size, you just edit the line int size = 5; and recompile.

But pretty soon — say, during week 2 — you want a way for the user to enter the size of the square, without having to recompile. You're probably not ready to muck around with argv. You're probably not ready to read a line of text using fgets and convert it back to an integer using atoi. (You're probably not even ready to seriously contemplate the vast differences between the integer 5 and the string "5".) So — during week 2 of your first C programming class — scanf seems like just the ticket.

That's the "one particular use case" I was talking about. And if you only used scanf to read small integers into simple C programs during the second week of your first C programming class, things wouldn't be so bad. (You'd still have problems forgetting the &, but that would be more or less manageable.)

The problem is (though this is again my personal belief) that it doesn't stop there. Virtually every instructor of beginning C classes teaches students to use scanf. Unfortunately, few or no of those instructors ever explicitly tell students that scanf is a stopgap, to be used temporarily during that second week, and to be emphatically graduated beyond in later weeks. And, even worse, many instructors go on to assign problems, involving scanf, for which scanf is absolutely not a good solution, such as trying to do robust or "user friendly" input validation.

scanf's only virtue is that it seems like a nice, simple way to get small integers and other simple input from the user into your early programs. But the problem — actually a big, shuddering pile of 17 separate problems — is that scanf turns out to be vastly complicated and full of exceptions and hard to use, precisely the opposite of what you'd want in order to make things easy for beginners. scanf is only useful for beginners, and it's almost perfectly useless for beginners. It has been described as being like square training wheels on a child's bicycle.

How do professional C developers handle this mess?

Quite simply: by not using scanf at all. For one thing, very few production C programs print prompts to a line-based screen and ask users to type something followed by Return. And for those programs that do work that way, professional C developers unhesitatingly use fgets or the like to read a line of input as text, then use other techniques to break down the line to extract the necessary information.


In answer to your initial question, there's no good answer. One of the fundamental rules of scanf usage (a set of rules, by the way, that no instructor ever teaches) is that you should never try to mix scanf and getchar (or fgets) in the same program. If there were a good way to make your "Press [enter] to continue..." code work after having called scanf, we wouldn't need that rule.

If you do want to try to flush the extra newline, so that a later call to getchar will work, there are several questions here with a bunch of good answers:


There's one more unrelated point that ends up being pretty significant to your question. When C was invented, there was no such thing as a GUI with multiple windows. Therefore no C programmer ever had the problem of having their output disappear before they could read it. Therefore no C programmer ever felt the need to write printf("Press [enter] to continue..."); followed by getchar(). I believe (another personal belief) that it is egregiously bad behavior for any vendor of a GUI-based C compiler to rig things up so that the output disappears upon program exit. Persistent output windows ought to be the default, for the benefit of beginning C programmers, with some kind of non-default option to turn that behavior off for those who don't want it.

CodePudding user response:

Is scanf broken? No it is not. It is an excellent input function when you want to parse free form input data where few errors are to be expected. Free form means here that new lines are not relevant exactly as when you read/write very long paragraphs on a normal screen. And few errors expected is common when you read from files.

The scanf family function has another nice point: you have the same syntax when reading from the standard input stream, a file stream or a character string. It can easily parse simple common types and provide a minimal return value to allow cautious programmers to know whether all or part of all the expected data could be decoded.

That being said, it has major drawbacks: first being a C function, it cannot directly control whether the programmer has passed types meeting the format specifications, and second, as beginners are not consistenly hit on their head when they forget to control its return value, it is really too easy to make fully broken programs using it.

But the rule is:

  • if input is expected to be line oriented, first use fgets to get lines and then sscanf testing return values of both
  • only if input is expect to be free form (irrelevant newlines), scanf should be used directly. But never without testing its return value except for trivial tests.

Another drawback is that beginners hope it to be clever. It can indeed parse simple input formats, but is only a poor man's parser: do not use it as a generic parser because that is not what it is intended for.

Provided those rules are observed, it is a nice tool consistent with most of C language and its standard library: a simple tool to do simple things. It is up to programmers or library implementers to build richer tools.

I have only be using C language for more than 30 years, and was never bitten by scanf (well I was when I was a beginner, but I now know that I was to blame). Simply I have just tried for decades to only use it for what it can do...

  •  Tags:  
  • Related