Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C getchar vs scanf

Tags:

c

scanf

getchar

I am confused by a piece of code found in a function I am studying:

char GetCommand( void )
{
    char command;

    do {
        printf( "Enter command (q=quit, n=new, l=list):  " );
        scanf( "%c", &command );
        Flush();
    }
    while ( (command != 'q') && (command != 'n')
           && (command != 'l') );

    printf( "\n----------\n" );
    return( command );
}

void Flush( void ) {
    while ( getchar() != '\n' )
        ;
}

What I don't quite understand here is the usage of the Flush() function. I mean, the book I am reading explains it by saying that it prevents the user from inputting more than a single character and then having that character read when they are prompted for input the 2nd time.

What I don't understand is how Flush() is preventing this from happening. It doesn't DO anything. All it is is a while command. (While this is true......what?????) Doesn't make sense.

like image 625
startuprob Avatar asked Sep 04 '10 02:09

startuprob


People also ask

Is Getchar better than scanf?

command = getchar(); but it's actually a generally bad example as it does not handle End Of File well. In general scanf is best forgotten; fgets and sscanf work much better as one is responsible for getting the input and the other for parsing it.

Why is Getchar used after scanf?

We can add a getchar() after scanf() to read an extra newline.

When should we use Getchar in C?

The getchar function is part of the <stdio. h> header file in C. It is used when single character input is required from the user. The function reads the input as an unsigned char ; then it casts and returns as an int or an EOF .

What does getchar () do in C?

getchar is a function in C programming language that reads a single character from the standard input stream stdin, regardless of what it is, and returns it to the program. It is specified in ANSI-C and is the most basic input function in C.


2 Answers

getchar() has the side effect of removing the next character from the input buffer. The loop in Flush reads and discards characters until - and including - the newline \n ending the line.

Since the scanf is told to read one and only one character (%c) this has the effect of ignoring everything else on that input line.

It would probably be more clear if the scanf was replace with

command = getchar();

but it's actually a generally bad example as it does not handle End Of File well.

In general scanf is best forgotten; fgets and sscanf work much better as one is responsible for getting the input and the other for parsing it. scanf (and fscanf) try to do too many jobs at once.

like image 81
msw Avatar answered Oct 30 '22 11:10

msw


getchar reads one character from standard input. If you put it in a while loop, it will continue to read one character at a time until the condition is false.

What the Flush function is doing is reading until it encounters a newline (\n). This is the character produced when the user hits the enter key.

So, the code you gave will read one character (I'm unclear on why it uses scanf for this instead of simply getchar, which would be faster), and then discards the rest of the input until the user hits enter.

If you were to feed this program foobar, it would take the f and discard the oobar in the Flush function. Without calling flush, the f could go to one scanf, and the second scanf would get the first o.

like image 27
Borealid Avatar answered Oct 30 '22 13:10

Borealid