In C language, the getch() function is used to read characters from the stdio stream, that is, reading a character from the console but not displaying it on the screen; the syntax is: "int getch(void)" , returns the characters read.
getch() function in C language
Function: Read characters from the stdio stream, that is, from the console Read a character but not display it on the screen
Usage:
int getchar(void);
This function is a non-echo function. When the user presses a certain character, the function automatically reads it without Press Enter. Some C language command line programs will use this function to make games, but this function is not a standard function, so pay attention to portability!
Getch() under Windows is in the header fileconio.h
. To use the getch() function, you need to reference theconio
header file.
Code:
#include#include #include #include #define ESC 0x1B #define ENTER 0x0D #define SPACE 0x20 #define KEY_UP 72 //上 #define KEY_DOWN 80 //下 #define KEY_LEFT 75 //左 #define KEY_RIGHT 77 //右 int KEY_EXIT_STATU = 0 ; int KEY_ENTER_STATU = 0 ; int KEY_SPACE_STATU = 0 ; int KEY_UP_STATU = 0 ; int KEY_DOWN_STATU = 0 ; int KEY_LEFT_STATU = 0 ; int KEY_RIGHT_STATU = 0 ; char ch ; int get_value() ; int main(void) { int i = 0; while(1) { get_value(); } return 0 ; } int get_value() { ch = getch() ; system("cls"); switch(ch) { case ESC : KEY_EXIT_STATU = 1 ; printf("退出\n") ; break ; case ENTER :KEY_ENTER_STATU = 1 ; printf("回车\n") ; break ; case SPACE : KEY_SPACE_STATU = 1 ; printf("空格\n") ; break ; case KEY_UP:case 'w' : KEY_UP_STATU = 1 ; printf("上\n") ; break ; case KEY_DOWN:case 's' : KEY_DOWN_STATU = 1 ; printf("下\n") ; break ; case KEY_LEFT:case 'a' : KEY_LEFT_STATU = 1 ; printf("左\n") ; break ; case KEY_RIGHT:case 'd': KEY_RIGHT_STATU = 1 ; printf("右\n") ; break ; } }
Note:
getchar has an int return value. When the program calls getchar, the program waits for the user to press the key and input the user. The characters are stored in the keyboard buffer until the user presses Enter (the Enter character is also placed in the buffer). When the user types Enter, getchar starts reading one character at a time from the stdio 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 the characters entered by the user are echoed to the screen.
If the user presses return If more than one character was entered before, the other characters will be retained in the keyboard buffer, waiting for subsequent getchar calls to read.
That is to say, subsequent getchar calls will not wait for the user to press a key, but directly read the buffer The characters in the buffer area will not wait for the user to press the key until the characters in the buffer area are read.
The basic functions of getch and getchar are the same. The difference is that getch obtains the key value directly from the keyboard without waiting for the user to press Enter. , as long as the user presses a key, getch will return immediately. The return value of getch is the ASCII code entered by the user. If an error occurs, it returns -1. The entered characters will not be echoed on the screen.
The getch function is often used for program debugging During debugging, relevant results are displayed in key positions for viewing, and then the getch function is used to pause the program. When any key is pressed, the program continues to run.
Recommended: "c Language Tutorial》
The above is the detailed content of What is the usage of getch() in C language?. For more information, please follow other related articles on the PHP Chinese website!