getch() 函数定义在 conio.h 头文件中。它是一个字符输入函数,在用户从键盘按下任何键之前,它会一直保持输出屏幕。即使它不回显按下的字符。
int getch(void);
getch() 函数不接受任何参数。它没有缓冲区来存储程序中的输入字符。
在 getch() 函数中,键盘输入的字符作为输出返回。
| 输入 | 返回值 |
|---|---|
| 如果按键 | 按下的字符 |
#include <stdio.h>
#include <conio.h>
int main()
{
printf("Enter a key to exit the console screen.\n");
getch();
return 0;
}
输出
Enter a key to exit the console screen.
#include <stdio.h>
#include <conio.h>
int main()
{
char ch;
printf(“Press any character: ”);
ch = getch();
printf(“\nPressed character is: %c”, ch);
}
输出
Press any character: Pressed character is: a