Function Name: getch ();
Function: one character is not displayed on the console.
Function Name: getchar ();
Function: read characters from the stdin stream
Function Name: scanf ();
Benefits:
Getchar has an int type return value. WhenProgramWhen getchar is called, the program waits for the user to press the key. The characters entered by the user are stored in the keyboard buffer until the user presses enter (the carriage return character is also placed in the buffer ).
The Return Value of the getchar function is the ASCII code of the first character entered by the user. If an error occurs,-1 is returned and the characters entered by the user are displayed on the screen.
If you enter more than one character before pressing enter, other characters will be kept in the keyboard cache area (including spaces and enter keys), waiting for subsequent getchar calls to read. that is to say, subsequent getchar calls will not wait for the user to press the key, but will directly read the characters in the buffer until the characters in the buffer are read as the key.
Getch gets the key value directly from the keyboard without waiting for the user to press the Enter key. If the user presses a key, getch immediately returns the value. The return value of getch is the ASCII code entered by the user, and-1 is returned if an error occurs. the entered characters are not displayed on the screen.
The getch function is often used in program debugging. During debugging, relevant results are displayed in key locations to be viewed. Then, you can use the getch function to pause the program. When you press any key, the program continues to run.
The scanf () function is the same as the getchar function. Both functions can read the enter and space keys in the keyboard buffer.
Getchar press Enter when entering data, and also press enter into the keyboard buffer.
Scanf () press Enter when entering data, and also press enter to enter the keyboard buffer.
Getch () does not need to press Enter when entering data. If you press a key, getch returns immediately.
Therefore, to prevent scanf and getchar from reading unnecessary characters. Before getchar and scanf
We 'd better clear the Keyboard Buffer first.
Rewind (stdin); // the C language function is used to clear the keyboard buffer ..
From: http://blog.sina.com.cn/s/blog_692df23e0100n5yt.html