Getchar does not stop when using scanf

I find it hard to understand 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 workaround that 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 does scanf do? Why does scanf do this?
2. Why does my work work?
3. What is a good way to emulate the following Python code:

 raw_input("Type Enter to continue") 
+7
source share
5 answers

Input is only sent to the program after entering a new line, but

 scanf("%s", command ); 

leaves a newline in the input buffer, since the %s (1) format stops when the first space character occurs after some non-space, getchar() then immediately returns this newline and does not need to wait for additional data to be entered.

Your workaround works because it clears the new line from the input buffer before calling getchar() again.

To emulate the behavior, 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 very unsafe, you should limit the input to what your buffer can hold with the field width, scanf("%99s", command) will read at most 99 ( sizeof(command) - 1) ) characters in command , leaving space for a 0-terminator.

+9
source

Space is the separator for the 5y3% s format specifier, and the new line is treated as a space, so it remains buffered. The console input is usually line-oriented, so a subsequent call to getchar () will immediately return because the "line" remains buffered.

 scanf("%s", command ); while( getchar() != '\n' ){ /* flush to end of input line */ } 

Similarly, if you use getchar () or% c to get a single character, you usually need to clear the string, but in this case the character entered may be a new line, so you need a slightly different solution:

 scanf("%c", ch ); while( ch != '\n' && getchar() != '\n' ){ /* flush to end of input line */ } 

similarly for getchar ():

 ch = getchar(); while( ch != '\n' && getchar() != '\n' ){ /* flush to end of input line */ } 
Of course, the smart thing is to turn these solutions into standalone specialized input functions that you can reuse, and also use as a place to put common input validation and error checking code (as in the answer of Daniel Fisher, who reasonably validates for EOF - you would usually like to avoid duplication of these checks and error handling around the world).
+2
source

I would prefer to use fgets first and then use sscanf to parse the input. I have been doing such things for so long, and the behavior was more predictable than a simple scanf .

+2
source

Well, I have something simpler: add another getchar() problem ...!

-one
source

after entering the command, enter the stdin command.

 fflush(stdin); 

but flushing the input stream leads to undefined behavior (although the Microsoft C library defines the behavior as an extension).

-2
source

All Articles