C getch()

getch() 函数定义在 conio.h 头文件中。它是一个字符输入函数,在用户从键盘按下任何键之前,它会一直保持输出屏幕。即使它不回显按下的字符。


int getch(void);  


getch() 参数: 

getch() 函数不接受任何参数。它没有缓冲区来存储程序中的输入字符。

getch() 返回值

在 getch() 函数中,键盘输入的字符作为输出返回。

输入 返回值
如果按键 按下的字符

getch() 示例

示例 1:getch() 函数在 C 语言中如何工作?


#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.

示例 2:getch() 在 C 语言中如何工作?


#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