Introduction :
In C programming language, the getch() function is a useful function for getting input a single character from the keyboard without display it to the screen. Normally, the user has to press the “Enter” key after enters the input. But in many programs like games or command-line utilities, getch() function is helpful because without “Enter” key it reads a single character from the keyboard.
In this article, I shall discuss with you how to use getch() function in C programming language. Here, I also show you the syntax and usage of getch() function.
What is getch() function :
The getch() function is defined in the conio.h library. It reads a single character from the keyboard and returns its ASCII value as an integer. In the beginning, you have to know that getch() function is not a part of the standard library of C programming language. The getch() is a non-standard function that is generally used in DOS and Windows environments. On Linux or Unix systems, getchar() function can be used instead.
To use the getch() function, you need to include the “conio.h” header file at the beginning of your program. The “conio.h” header file contains the declaration of the getch() function. Now, you can call the getch() function whenever you want to input a single character from the keyboard. It does not require any input arguments and returns the ASCII value of the character that was input by the user.
Now, you can use the input character in your program as you want. For example, you can show the input character to the screen or use it to make decisions in your program. The getch() function does not display the input character on the screen. If you want to display the character which the user enters from the keyboard, you have to use the printf() function or other display functions.
Advantages of getch() function :
The getch() function allows you to read a single character from the keyboard without waiting for the user to press the “Enter” key. This function is useful when you need to get input from the user quickly or where you want to control the flow of the program based on the user’s input.
Using getch() function, you can read special keys like the function keys, the arrow keys and other non printable keys. Using scanf() or gets() functions, you cannot read these keys.
Disadvantages of getch() function :
The main disadvantages of using the getch() function is that it is not a standard function in C programming language. The function is only available in certain compilers and operating systems. So when you write a program using getch() function, that may not run on all the systems.
The getch() function will not work well with Unicode characters. If you need to read Unicode characters from the keyboard, you should use a different function like getwchar().
Syntax of the getch() function :
The following code is the syntax of the getch() function in C programming language.
int getch(void);
The getch() function takes no arguments and returns the ASCII value (integer) of the character which input by the user.
Different types of program using getch() function :
The getch() is a versatile function that can be used for a variety of purposes in C programming language. I shall show you the following usage of getch() function in C programming language.
Input character by getch() function and display the character in C programming language :
In this program, I shall show you how to take input (single character) using getch() function and display the character in C programming language. Here, getch() is used to read a single character from the keyboard. It stores the character in char variable (ch). Using printf() function, you can print the character on console screen.
To run this program, you have to install VS Code on your PC. Now, open VS Code and create a file with .c extension. Then, copy the below code and paste in your c file, which you just created.
/* Developd by Puskar Jasu*/
#include <stdio.h>
#include <conio.h>
int main()
{
char ch;
printf("Please enter a character\n");
ch = getch();
printf("\nYou just entered %c\n", ch);
return 0;
}
Output :
After running the program, it asks the user to enter a character. It reads the character using the getch() function. Then, using the printf() function it displays the character on the console screen. You can see the output like below image.

Stop the termination of program by getch() function in C programming language :
Sometime, you see a C program immediate stop after execution all the code of the program. So you can’t see the output of the program. In this situation, you can use getch() function for stopping the program until input a character. When you input a character, the program closes without display the character you entered. In this case, you have to enter getch() function before the “return” statement.
In the above program, when you run the exe file of the program you can’t see the output. Because the program stops immediately after run the program. So if you want to see the output add getch() function before “return” statement like below code.
/* Developd by Puskar Jasu*/
#include <stdio.h>
#include <conio.h>
int main()
{
char ch;
printf("Please enter a character\n");
ch = getch();
printf("\nYou just entered %c\n", ch);
printf("press any key to stop the program\n");
getch(); // You have to add this code to stop the termination of the program
return 0;
}
Input password by getch() function and display the password in C programming language :
In this program, I use getch() function to read password input from the user without display the password on the screen. This is generally used in login screen to protect the password from being seen by other people. Here, you can see the password is not displayed on the screen and asterisk (*) is printed for each character user has typed. For this program, I use while loop.
The loop stops when user presses the “Enter” key (ASCII code 13) and the password is stored in the password array. The tab key (ASCII code 9) and the space key (ASCII code 32) don’t allow in password so when user presses the tab key and space key, the input is ignored by the program. The backspace key (ASCII code 8) is also handled in the program, so the user can correct the wrong password enter by himself.
This is an example of creating password program using getch() function in C programming language. If you want to run this program on your PC, create a c file in VS Code. Then, copy the following code and paste in your c file that you have created in VS Code.
/* Developd by Puskar Jasu*/
#include <stdio.h>
#include <conio.h>
int main()
{
char password[20];
int i = 0;
printf("Please enter your password\n");
while (1)
{
char c = getch();
if (c == 13)
{
password[i] = '\0';
break;
}
else if (c == 9)
{
password[i] = '\0';
}
else if (c == 32)
{
password[i] = '\0';
}
else if (c == 8 && i > 0)
{
i--;
printf("\b \b");
}
else
{
password[i] = c;
i++;
printf("*");
}
}
printf("\n%s is Your password\n", password);
return 0;
}
Output :
After running the program on your PC, you see the below image as output.

Menu selection by getch() function in C programming language :
The getch() function can be used for menu selection input from the user. This is often used in console-based programs to allow the user select an option from a menu. In this program, I show you how to create menu selection program using getch() in C programming language. Where the user selects an option from 1 to 4. In the program, I store the user selection in “choice” variable.
In the while loop, give a condition that when user presses 4 the loop will stop. Here, you have to store user selection in “choice” variable. Because getch() function stores ASCII value so I subtracted by ‘0’ so that I get the proper selection. You can use this code in your program and see the output.
/* Developd by Puskar Jasu*/
#include <stdio.h>
#include <conio.h>
int main()
{
int choice;
while (choice != 4)
{
printf("Select a sport you like to play:\n");
printf("For Football press 1\n");
printf("For Cricket press 2\n");
printf("For Volleyball press 3\n");
printf("If you want to exit press 4\n");
choice = getch() - '0';
switch (choice)
{
case 1:
printf("Your favourite sport is Football.\n");
break;
case 2:
printf("Your favourite sport is Cricket.\n");
break;
case 3:
printf("Your favourite sport is Volleyball.\n");
break;
case 4:
printf("You want to stop the program.\n");
break;
default:
printf("You select wrong option.\n");
}
}
return 0;
}
Output :
You can see the below image as output, if you run this program on your PC.

Take input for graphics programs by getch() function in C programming language :
The getch() function is also used in graphics programs to wait for user input before closing the window or moving to the next frame. The following is an example of how getch() function can be used in a simple graphics program. For graphics program, you have to use the graphics.h library of C programing language. First initialize the graphics window by initgraph() function.
Then, using the circle() and outtextxy() function draw a green circle and a text message on the window. The getch() function waits for the user to press any key and the closegraph() function closes the graphics mode. Here getch() function is used to pause the program until the user presses a key, which allows the user to view the graphics output before the window is closed.
Without getch() function, the program closes the graphics window immediately after drawing the circle and the text message. This program is specific to Turbo C++ Graphics and may not work in other graphics libraries and platforms. So if you run this program on your PC, first install Turbo C++ IDE. Then create a c file with .c extension and copy paste the below code in your c file.
You have to know how to copy paste in Turbo C++ IDE. To run graphics program, you have to know how to run graphics programs in turbo C++ on your PC.
/*Developed by Puskar Jasu*/
#include <graphics.h>
#include <conio.h>
int main(void)
{
int graphic_driver = DETECT, graphic_mode;
initgraph(&graphic_driver, &graphic_mode, "//turboc3/bgi");
setcolor(GREEN);
circle(320, 240, 100);
outtextxy(170, 350, "Press any key to close the graphic window");
getch();
closegraph();
return 0;
}
Output :
After running the program in Turbo C++, you can see the following output on your PC.

Interact with graphics programs by getch() function in C programming language :
In graphics programming, getch() can be used to implement keyboard input for user interactions with the graphical interface. For example, getch() can be used to implement key-based events like moving a shape and changing its color. In the below example, I show you how to move and change color of circle by C graphics programming language.
Here, I draw a circle in the center of the screen. Then, the program enters an infinite loop and now user presses a key which read by getch() function and according to user input update the position of the circle and change its color. If the “enter” key is pressed, the loop end and the program closed. By the arrow keys, user can move the circle.
The cleardevice() function is used to erase the previous frame and the circle() function is used to draw the updated circle. Now copy the below code and use it in your c file of Turbo C++.
/*Developed by Puskar Jasu*/
#include <graphics.h>
#include <conio.h>
int main(void)
{
int x, y, i;
char c;
int graphic_driver = DETECT, graphic_mode;
initgraph(&graphic_driver, &graphic_mode, "//turboc3/bgi");
x = getmaxx() / 2;
y = getmaxy() / 2;
i = 1;
while (1)
{
cleardevice();
setcolor(i);
circle(x, y, 50);
c = getch();
if (c == 72)
{ // press up arrow key for move up
y = y - 2;
}
else if (c == 80)
{ // press down arrow key for move down
y = y + 2;
}
else if (c == 75)
{ // press left arrow key for move left
x = x - 2;
}
else if (c == 77)
{ // press right arrow key for move right
x = x + 2;
}
else if (c == 13)
{ // press enter key for stop program
break;
}
i++;
if (i == 16)
i = 1;
}
closegraph();
return 0;
}
Output :
Now, run the above program on your PC and see the following output.

Conclusion :
In summary, the getch() function in C programming language is a useful function for reading a single character from the keyboard without waiting for the user to press the “Enter” key. It is commonly used in console based applications where keyboard input is required without the user having to press the “Enter” key. However, it is not a standard function in C programming language and may not work on all systems.
After reading this article, you would understand how to use getch() function in your C program. Thank you for visiting my site.