C Programming

Getch Function in C

For the many programs that are written in the C programming language, we have to take input from the user. The user input is some keys or values that we type from the keyboard. If we have to get the user input from the keyboard as a character, we use the getch() function. This function comes under the header file of “conio.h”. Getch() method has the specialty to pause the console until the enter key is pressed which means that it returns the ASCII character as soon as it is pressed from the keyboard since it has no buffer for the storage of this (ASCII) value. The applications of such functions can be found in the system where we do not want the system to show the typed character from the keyboard or to keep displaying the input after we press the enter key such as the use of passwords and the input that are meant to be hidden like the pins for the ATM accounts.

Procedure

We will look into the concept of the getch() function that is from the header file “conio. h” in the Microsoft Visual Studio compiler for the C programming language. We will discuss the syntax for this function and will try to execute some examples using the learned syntax of this function.

Syntax

The syntax for the getch() method is quite simple. All we have to do is to declare a data type for this function and then simply call this function. Keep in mind that this function does not accept any input arguments/parameters that are user-defined.

$ int getch(void)

Example 1:

In this example, we start with a basic example to execute a program to verify the working of the syntax for the getch() function. This program takes the input from the user in the ASCII character and returns the ASCII value before we press the enter key. Let’s start this example by creating a project in the Visual Studio compiler first. We add a source file to this project so that we may add this project to the path of the C directory files by saving the project with the name “.c”. Then, we import the fundamental header files for this program. As we already know, our program utilizes the function such as displaying the output, which is printf(). And since we take the character input from the user, we use the getch() function to access these functions in our program. Import the following header files as follows:

$ # include <stdio.h>

$ # include <conio.h>

Now, to implement the example in the project, we declare a function with the name “main” and “int” as the return type of this function. In this main function, we make a call to the printf() method. We pass the format specifier as “%c” in its parameters with the getch() function for the input of the character. Then, we return the 0 after we come out of the function. This explanation can be re-written in the form of the code as follows:

#include <stdio.h>  

#include <conio.h>  

int main()

{

    printf(" enter the user defined value: %c", getch())

    return 0;

}

When we execute the previously-mentioned code in the C compiler, the output displays a window that asks us to give the input for the character from the keyboard. We press any key from the keyboard and the output returns the value of ASCII for that key and the console remains open until we press the enter key.

Example 2:

We already discussed that the getch() function does not use any buffer to store the value and rather immediately returns the value in the function. We may use this attribute of the function and write a program that holds the output screen or console until a character key is entered as input from the keyboard. To implement this, we use the same functions that we used in the previous example but the method would be slightly different. Previously, we made a function call of the getch() method in the arguments of the printf() method. In this example, we call them separately. Open and create a project in the Visual Studio C compiler. Import the following two header file as follows:

$ # include <stdio.h>

$ # include <conio.h>

Then, declare the main function with return type “integer”. Call the printf() method in the function and print the sentence “enter the character”. Then, call the getch() method and come out of the main function after returning the value 0.

#include <stdio.h> 

#include <conio.h>  

int main()

{

    printf("Enter any key if you want to exit the screen.\n");

    getch();

    return 0;

}

In the output, the screen holds until we press any key from the keyboard.

Example 3:

This example lets us develop a program that takes the hidden password from the user-defined input. For this example, we import the following header files:

$ # include <stdio.h>  

$ # include <conio.h>

$ # include <string.h>

$ # include <dos.h>

Then, we declare a function with the name “main” and return the type as “void”. We first declare an array of size 50 with data type “char” in the main. We initialize this array by taking the value from the user. To do so, we initialize a variable with a data type integer. Then, using this variable, we run a “for loop” that has an increment of +1 and initial value of 0. We want the loop to break at the value of variable less than 10.

In this “for loop”, we assign the already declared array the getch() function to take the input string from the user. Coming out of the loop, we print the user input in the form of “*”. To display this hidden password, we assign the array with the value of “enter” as “\0”. Then, run a for loop with the condition to display the entered keys from the keyboard until the input string is not equal to the “\0” enter key. Then, we print this input array and call the getch() function again after coming out of the loop. This displays the hidden password from the user. This can be done by writing and executing the following program:

#include<stdio.h> 

#include<string.h> 

#include<dos.h> 

#include<conio.h> 

void main()

{char array[50];

    int i;

    printf(" Enter any password: ");

    for (i = 0;i < 10; i++){

       array[i] = getch();

        printf("*");

    } array[i] = '\0';

    printf("\n");

    printf(" the hidden password: ");

    for (i = 0; array[i] != '\0'; i++)r

    { printf("%c", array[i]);

    } getch();

}

Conclusion

This article covered the basic concept of the getch() function in C. It gives a detailed explanation of the syntax of the function and also shows how we can write the various programs for the different examples using the getch() function. We hope that this article lets you clear your ambiguities regarding the topic.

About the author

Omar Farooq

Hello Readers, I am Omar and I have been writing technical articles from last decade. You can check out my writing pieces.