易百教程

getch()和getche()之间有什么区别?

getch()函数从键盘上读取一个字符。它不使用任何缓冲区,所以输入的数据不会显示在输出屏幕上。
getche()函数从关键词中读取一个字符,但数据会显示在输出屏幕上。按Alt+f5可以看到输入的字符。
让我们看一个简单的例子 -

#include<stdio.h>  
#include<conio.h>  
int main()  
{  

 char ch;  
 printf("Enter a character ");  
 ch=getch(); // taking an user input without printing the value.  
 printf("value of ch is %c",ch);  
 printf("Enter a character again ");  
 ch=getche(); // taking an user input and then displaying it on the screen.  
  printf("value of ch is %c",ch);  
 return 0;  
}

运行结果如下:

Enter a character
value of ch is a
Enter a character again a
value of ch is a

在上面的例子中,通过getch()函数输入的值不显示在屏幕上,而通过getche()函数输入的值却显示在屏幕上。