getch() and getche() functions of conio.h in C

In this article, we are going to learn about the pre-defined functions getch() and getche() of conio.h header file and use them with the help of their examples.
Submitted by Manu Jemini, on March 14, 2018

Now, these two functions are very useful. Most of the time, the output of the program flicks away from the user’s eyes. As we don’t want to unless the user doesn’t care. What we, in fact, is that when the last output of the program comes up, the output window should freezes-up.

<conio.h> - getch() function in C

getch() function is used, to take a single byte of input from a user. We don’t care about the input anyway, all we want is, that user should get some time to read or the window should wait till the user wants it to be there. When the user presses any key on the keyboard the function ends and the program ends.

getch() function Example in C

#include <stdio.h>
// to use 'getch() function
#include <conio.h>

int main() {
  // message before pressing key
  printf("Now the program is waiting for user to press a key from keyboard.\n");

  // using getch() function for reading
  // the single byte character
  getch();
  return 0;
}

Output

conio.h - getch() function Example in C

<conio.h> - getche() function in C

The getche() function is almost similar but the little wider as it take any character or alphanumeric input. But the different thing it does, is it prints out the input that the user has given.

These functions are available in the condo.h file. You should include it if you wanna use these functions.

getche() function Example in C

#include <stdio.h>
// to use 'getche()' function
#include <conio.h>

int main() {
  // message for user
  printf("The program is waiting for user to press a key from keyboard.\n");

  // press any character and alpha-numeric key
  // and the function will print it.
  getche();
  return 0;
}

Output

conio.h - getche() function Example in C



Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.