Interviews Questions, Algorithms, Aptitude, C Interview Program, C Theory Question, Aptitude Tricks, Test Series,

Saturday 1 June 2019

getch() vs getche()


The getch() function reads a single character from the keyboard. It doesn't use any buffer, so entered data will not be displayed on the output screen.

The getche() function reads a single character from the keyword, but data is displayed on the output screen. Press Alt+f5 to see the entered character.

Let's see a simple example

#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("\nvalue of ch is %c",ch);  
 printf("\nEnter a character again ");  

 ch=getche(); // taking an user input and then displaying it on the screen.  

  printf("\nvalue of ch is %c",ch);  
 return 0;  
}  

Output:

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

In the above example, the value entered through a getch() function is not displayed on the screen while the value entered through a getche() function is displayed on the screen.

0 comments:

Post a Comment