Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

getchar does not stop when using scanf

Tags:

c

getchar

I have a difficulty understanding getchar(). In the following program getchar works as expected:

#include <stdio.h>


int main()
{
    printf("Type Enter to continue...");
    getchar();
    return 0; 
} 

However, in the following program, getchar does not create a delay and the program ends:

#include <stdio.h>

int main()
{
    char command[100];
    scanf("%s", command );
    printf("Type Enter to continue...");
    getchar();
    return 0; 
} 

I have the following weired workaround, which works, but I don't understand why:

#include <stdio.h>

int main()
{
    char command[100];
    int i;
    scanf("%s", command );
    printf("Type Enter to continue...");
    while ( getchar() != '\n') {
      i=0; 
    }
    getchar();
    return 0;    
}

So my questions are:
1. What is scanf doing? Why does scanf do this ?
2. Why is my work around working?
3. What is a good way to emulate the following Python code:

raw_input("Type Enter to continue")
like image 490
oz123 Avatar asked Sep 29 '12 15:09

oz123


People also ask

Why is Getchar used after scanf?

scanf is a C function to read input from the standard input until encountering whitespace, newline or EOF while getchar is a C function to read a character only from the standard input stream(stdin), which is the keyboard. Thus, this is the main difference between scanf and getchar.

How do I disable Getchar?

You have to declare the 'read' variable as int, not char - else you might very well get an infinite loop. It should exit if you kill it with CTRL+C, or end the input with CTRL+Z.

How do I stop scanf from reading?

If you use a %d format specifier, a decimal point will stop the reading. Furthermore, if you use a %f format specifier and the user types an integer, scanf will convert the integer to a floating point value.

Does scanf stop at newline?

scanf() reads input until it encounters whitespace, newline or End Of File(EOF) whereas gets() reads input until it encounters newline or End Of File(EOF), gets() does not stop reading input when it encounters whitespace instead it takes whitespace as a string.


1 Answers

The input is only sent to the program after you typed a newline, but

scanf("%s", command );

leaves the newline in the input buffer, since the %s(1) format stops when the first whitespace character is encountered after some non-whitespace, getchar() then returns that newline immediately and doesn't need to wait for further input.

Your workaround works because it clears the newline from the input buffer before calling getchar() once more.

To emulate the behaviour, clear the input buffer before printing the message,

scanf("%s", command);
int c;
do {
    c = getchar();
}while(c != '\n' && c != EOF);
if (c == EOF) {
    // input stream ended, do something about it, exit perhaps
} else {
    printf("Type Enter to continue\n");
    getchar();
}

(1) Note that using %s in scanf is very unsafe, you should restrict the input to what your buffer can hold with a field-width, scanf("%99s", command) will read at most 99 (sizeof(command) - 1)) characters into command, leaving space for the 0-terminator.

like image 171
Daniel Fischer Avatar answered Sep 29 '22 11:09

Daniel Fischer