C Programming in linux -- getchar () and linuxc programming getchar
Getchar
Function Name: getchar
Function: read characters from the stdin stream
Usage: int getchar (void );
Note:
Getchar has an int type return value. When the program calls getchar, the program waits for the user to press the key, user-entered characters are stored in the keyboard buffer until you press enter (the carriage return character is also placed in the buffer ). After you press enter, getchar starts to read one character each time from the stdin stream. 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 display the characters entered by the user back to the screen. If you enter more than one character before pressing enter, other characters will be kept in the keyboard cache and will be read by getchar later. 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.
The basic functions of getch and getchar are the same. The difference is that getch directly obtains the key value from the keyboard. If you do not wait for the user to press the Enter key, getch returns immediately, the return value of getch is the ASCII code entered by the user. If an error occurs,-1 is returned. 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 for query. Then, the program is paused using the getch function. When you press any key, the program continues to run.
Demo:
#include <stdio.h>int main(void){ int c; /* Note that getchar reads from stdin and is line buffered; this means it will not return until you press ENTER. */ while ((c = getchar()) != '\n') printf("%c", c); return 0;}
Note: You can use the getchar () function to let the program wait for the programmer to press the keyboard to return to the editing interface after debugging and running. Usage: Add getchar () before return 0 to the end of the main function (); you can.