Search code examples
cinputscanfuser-inputstandards

How to definitely solve the scanf input stream problem


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.


Solution

  • but what is the purpose of scanf then?

    An excellent question.

    Is it simply a 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 decent reason why all beginners are taught to use scanf, though. 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 that first week, 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" at all.) 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 (though this is again my personal belief) is that it doesn't stop there. Virtually every instructor of beginning C classes teaches students to use scanf. Unfortunately, few or none 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 more advanced problems, involving scanf, for which it 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 full 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 might 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.