You are here

getch in C

getch in C language: getch function prompts a user to press a character and that character isn't printed on screen, getch header file is conio.h. This function is not a part of standard C library.

C programming code for getch

  1. /* Function getch in C example */
  2. #include<stdio.h>
  3. #include<conio.h>
  5. main()
  6. {
  7.    printf("Waiting for a character to be pressed from the keyboard to exit.\n");
  9.    getch();
  10.    return 0;
  11. }

When you will run this program, the program will exit only when you press a character, note that we are talking about a character so try pressing numlock, shift key etc (program will not exit if you press these keys) as these are not characters. Also try the above program by removing getch(), in this case program will exit without waiting for a character being pressed from keyboard.

How to use getch in C++

  1. #include<iostream.h>
  2. #include<conio.h>
  4. main()
  5. {
  6.    cout << "Enter a character";
  7.    getch();
  8. }

getch in Dev C++ compiler

getch library function works in Dev C++ compiler, also note that it doesn't support all functions of conio.h as Turbo C does.

getchar in c

  1. #include<stdio.h>
  3. main()
  4. {
  5.    int c;
  7.    c = getchar();
  9.    putchar(c);
  11.    return 0;
  12. }

Common use of getch is that you can view the output (if any) of a program without having to open the output window if you are using Turbo C compiler or if you are not running your program from command prompt.