C getch()

The getch() function is defined in the conio.h header file. It is a character input function which holds the output screen untill the user passes any key from the keyboard.Eventhough it does not echo the pressed character.


int getch(void);  


getch() Parameters: 

The getch() function does not take any parameter.To store the input character in a program it has no any buffer area.

getch() Return Value

In getch() function the keyboard entered characters are returned as output.

Input Return Value
if keypressed pressed character

Examples of getch()

Example 1: Working of getch() function in C?


#include <stdio.h>
#include <conio.h>

int main()  
{  
   printf("Enter a key to exit the console screen.\n");  
   getch();   
   return 0;  
}  

Output:


Enter a key to exit the console screen.

Example 2: How getch() works in C?


#include <stdio.h>
#include <conio.h>
int main()  
{
  char ch;
  printf(“Press any character: ”);
  ch = getch();
  printf(“\nPressed character is: %c”, ch);
}

Output:


Press any character: 
Pressed character is: a