How to Use getch() Function in C Programing Language

getch() function by c programming language

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 have to press the “Enter” key after enter the input by the user. But in many program, where require interactive input like games or command-line utilities, getch() function is helpful because without “Enter” key getch() function read a single character from the keyboard. The getch() function is defined in the conio.h library and It takes no arguments and returns ASCII value of the character that input by user. In this article, I shall discuss you how to use getch() function in C programming language.

In the beginning, you have to know that getch() function is not a part of the standard library of C programing language. The getch() is a non-standard function that is generally used in DOS and Windows environments. Therefore, it may not work in other environments and may require special library functions or include files.

Advantages of getch() function :

The getch() function allow 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 read special keys like function keys, arrow keys, and other non-printable keys. By 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 by using getch() function when you write a program, this may not run on all systems.

The getch() function is not work well with Unicode characters. If you need to read Unicode characters from the keyboard, you should use a different function like getwchar().

The syntax of the getch() function is show in below

int getch(void);

The getch() function reads a single character from the keyboard and returns its ASCII value as an integer.

Use of getch() function in C programming language :

In this article, I shall show you how to use the getch() function in C programming language. To use the getch() function in C programming language, you need to include the “conio.h” header file in your program. In “conio.h” header file contains the declaration of the getch() function. First you include the “conio.h” header file at the beginning of your program.

#include <conio.h>

Note that conio.h header file is specific to DOS and Windows systems. On Linux or Unix systems, getchar() function can be used instead.

In your program, call the getch() function whenever you want to input a single character from the keyboard. The function does not require any input arguments and returns the ASCII value of the character that was input by the user. Then 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 enter from the keyboard, you have to use the printf() function or other display functions. 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 programing language :

In this program I show you how you take input single character by getch() function and show the output by C programing language. Here getch() is used to read a single character from the keyboard, which is then stored in the “ch” variable.

char ch = getch();

The character is then print on screen using printf() function.

printf("%c", ch);

Now open VS Code and create a file with .c extension and copy the below code and paste in your c file, which you just created. For run this program you have to install VS Code in your pc.

/* Developd by Puskar Jasu*/
#include <stdio.h>
#include <conio.h>
int main() {
   char ch;
   printf("Please enter a character: ");
   ch = getch();
   printf("\nYou just entered: %c\n", ch);
   return 0;
}

Output :

After run the program, asked the user to enter a character, reads the character using the getch() function and using the printf() function displays the character on the screen. You can see the output like below image.

Read by getch() and display in c programing language

Stop the termination of program by getch() function in C programing language :

Sometime you see a C program immediate stop after execution all the code of the program. So you can not see the output of the program. In this situation, you can use getch() function for stop the program until input a character. When you input a character the program close 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 not see the output. Because the program stop immediately after run the program. So if you want to see the output add getch() 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: ");
   ch = getch();
   printf("\nYou just entered: %c\n", ch);
   getch();             //You have to add this code for stop the termination of the program
   return 0; 
}

Input password by getch() function and display the password in C programing 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 password is not displayed on the screen and asterisk (*) are printed for each character user has typed.

For this program I use while loop. The loop stop when user press 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) are not allow in password so when user press 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. If you run this program in your pc, create a c file in VS Code. Then copy the following code and paste in your c file, you have create 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: ");
    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 run the program in your pc, you see the below image as output.

take password by getch() in c programing 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 you create menu selection program using getch() in C programing language. Where the user select 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 press 4 the loop will stop. In the while loop user selection store in choice variable. Because getch() function store ASCII value so I subtraction 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 see the below image as output, if you run this program in your pc.

menu selection by getch() in c programing language

Take input for graphics programs by getch() function in C programing 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 and conio.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 close the graphics mode.
Here getch() function is used for pause the program until the user press a key, which allows the user to view the graphics output before the window is closed. Without getch() function the program close 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 in your pc, first install turbo C ++. Then create a c file with .c extension and copy paste the below code in turbo C++ within your c file. For run graphics program, you have to know how to run graphics program in turbo C++ in 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 run the program in turbo C++ you see the following output in your pc.

getch() use in graphic program by c programing 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 enter an infinite loop and now user press a key which read by getch() function and according to user input update the position of the circle and change it’s 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 :

After run the above program in your pc, you see the following output in your pc.

change circle by getch() in c programing language

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 read this article, you would understand how to use getch() function in your C program.

Thank you for visit my site.

You can see my following project :

You can see my following program :

Scroll to Top