×

getch() function in C

getch() is a pre-define or built-in function present in the conio.h library. It returns the given character immediately without waiting for the enter key to be entered. By using getch() function, we can read a single character.

By this function, the output console is paused until a key is pressed. In turrbo c software, if we give getch() function, it shows the output window directly.

The function prototype style is with return values and without parameters. ie, int getch( void ). The return data type returns the ASCII value of the given character to the main function. It also accepts the hidden characters inputs, passwords, security pins, Atm pins etc. This is used in MS-DOS compiler ex: Turbo C.

Advantages of getch() function:

  1. A single character can be read using this function.
  2. The character which we entered is returned immediately without entering the enter key.
  3. By using this function, we can pause the output console.
  4. The given character won’t be visible on the output screen.

Disadvantages of getch() function:

  1. The getch() reads the single character. If you enter hi, it only reads h.

Example 1:

//  program to display the given character using getch() function.
#include <stdio.h>
#include<dos.h>
#include<conio.h> //including the header file
int main() // main function
{
    printf(" Enter the character ");
     printf("  \nThe Enter character is:  %c", getch() ); // function calling
    return 0;
}

Output:

Enter the character: 
The Entered character is: p
The input character is hidden on the output screen.

Example 2:

// program to print the given string using getch() function:
#include <stdio.h>
#include<dos.h>
#include<conio.h> //  including the library
int main()
{
    char a[11] = {0};// initilizing the string
    int i;
    printf(" Enter the string\n");
    for( i = 0; i < 11; i++)
   {
       a[i] = getch(); // calling getch() function to read the character
   }
   printf(" The entered string using getch() function is: %s", a);  //  printing the string
   return 0;
}

Output:

Enter the string:
The entered string is: JavaTpoint
The input won’t be displayed on the output screen.

Example 3:

// program to print the sorted array and pause the console output screen for a while.
#include <stdio.h>
#include<dos.h>
#include<conio.h> // including the header file
#define max 100
int main()
{
   int n, a[max], i, j, t; // declaring teh variables 
   printf("Enter the size of the array: ");
   scanf("%d", &n); // reading the array size
   printf(" \nEnter the array elements: ");
   for( i=0; i<n; i++) // reading the array elements
   {
       scanf("%d", &a[i]);
   }
   printf(" \nEntered array before sorting: "); // printing the array elements before sorting
   for( i=0;i<n; i++) 
   {
       printf("%d ", a[i]);
   }
     printf(" \nArray sorting starts:");
   for( i=0;i<n-1;i++) // sorting the array elements
   {
       for(j=0;j<n-i-1;j++)
       {
           if( a[j]>a[j+1])
           {
               t = a[j];
               a[j] = a[j+1];
               a[j+1] = t;
            }
       }
   }
   printf(" \nEntered array after sorting: "); // printing the sorted array
    for( i=0;i<n; i++)
   {
       printf("%d ", a[i]);
   }
  getch(); // holding the console output screen using getch() function.
   return 0;
}

Output:

Enter the size of the array: 10
Enter the array elements: 56 98 45 3 80 90 7 4 1 30
Entered array before sorting: 56 98 45 3 80 90 7 4 1 30  
Array sorting starts: 
Entered array after sorting: 1 3 4 7 30 45 56 80 90 98
In this program the output screen is paused until the enter key is entered.

Example 4:

// program to take the password and hiding on the output screen.
#include<stdio.h>  
#include<conio.h>  
void main()  
{  
    char w[10];  
    int i;  
    printf(" Please Enter the correct password: ");  
    for (i = 0; i < 10; i++)  
    {  
        // accepts the hidden password using the getch() function  
        w[i] = getch();  // function call
        printf("#"); // print the input chartered in the form of #  
    }  
    w[i] = '\0';  
    printf( "\n" );  
printf("The password which you entered on input screen: ");  
    for (i = 0; w[i] != '\0'; i++)  
    {  
        printf("%c", w[i]);  
}  
getch(); // holding the console output screen  
}

Output:

Please enter the correct password: ##########
The password which you entered on the input screen: JavaTpoint

Here the password is hidden on the input screen by using the getch() function.

The getch() function wont display the entered character onthe output screen. This is the advantage of the getch() function.


Related Topics

2f in C language

Float data type in c: Double-precision floating-point numbers with up to 17 significant digits are stored in the FLOAT data type. FLOAT is equivalent to C's double data type and IEEE...

3 minutes read.

Find a subarray with a given sum.

Find a subarray with a given sum. The simple solution is to recognize all subarrays one by one and to check each subarray's sum. The quick solution follows the following program. Algorithm: From...

4 minutes read.

Return array from function in C

Return array from function in C C programming does not require the return to a function of a whole array as an argument. However, you can return a pointer to an array without...

2 minutes read.

Execution flow of C program

There are various steps of the execution of the C program that is given below. The following step of the execution Write source codes Preprocess Compile Link edit Load Execute Editor or...

1 minute read.

Format Specifier in C

Format Specifier in C Format specifiers can be described as an operator that is used to print the data referred by any object or variable in combination with the printf ()...

3 minutes read.

Bank Account System in C using File Handling

This tells about the creation of bank account system using the C language and handling of files in C. Approach Let us see the approaches and the functions how they are covering...

5 minutes read.

Dynamic memory allocation

Dynamic memory allocation is used to allocate the memory at runtime. It has following function within <stdlib.h> header file. Function Syntax malloc() malloc (number *sizeof(int)); calloc() calloc (number, sizeof(int)); realloc() realloc (pointer_name, number * sizeof(int)); free() free (pointer_name); malloc() function The process...

2 minutes read.

How to install a C compiler in windows 10-64bit

Many C compilers can be installed on your pc. The most downloaded compilers include GCC, Turbo C++, Visual Studio, etc. This article includes the installation of the Turbo C++ compiler in...

6 minutes read.

Pure Virtual Function in C

Before knowing about the pure virtual function some of the important points about the virtual function in C++ are given below. In c++ the member function that is defined in the...

4 minutes read.

Hexadecimal to Binary in C

What is hexadecimal? Hexadecimal defines a sequence of numbers centered on-16, i.e., before assigning the next number to a new location, it describes a numbering scheme that includes 16 sequential numbers as basic...

2 minutes read.

Bitwise XOR Operator in C

What is Bitwise? As we know, bits are the smallest unit of a number and are mostly used in a computing system. So, in bitwise, we do operations on bits instead...

3 minutes read.

Functions in C

What is function? Functions are defined as the set of announcements which take inputs, perform operations and provide results. The operation of a function only performs when the function gets called....

4 minutes read.

Type Conversion in the C

The process of changing one data type to another in the C programming language is known as "type conversion." The type conversion method is only executed on data types that...

4 minutes read.

How to Merge Array in C?

Introduction Merging arrays is a common task for many developers but might be difficult for beginners of C programming. Fortunately, our guide will show you how to quickly merge arrays in...

6 minutes read.

Escape Sequence in C

An escape sequence is a sequence of character that used inside the string literal or character.  All the escape sequence is used with the backslash (\) symbol. The list of an...

1 minute read.

Exponential() in C

Introduction: In C language, there are available many types of functions. The exponential() function is one of them. It is a mathematical function. This article describes using the pow() property to...

3 minutes read.

Ftell() Function in C

Ftell(): In File Handling we have some special functions like Ftell(), Fseek(), rewind() etc.. while you are randomly accessing the file these functions play very important role and these functions...

3 minutes read.

Limitations of Inline Function in C

The compiler is unable to conduct inlining in two instances. It just accepts the inline definitions and produces space for the function in the same way it does for a...

3 minutes read.

C Math Library

C Math Library The <math.h> header defines various mathematical functions and one macro. Many functions are available in this library to take double as an argument and then return double as...

4 minutes read.

Stdio.h in C

Header files are used to make the programmer’s efforts a lot easier. In order to make the programming simple, there are a number of libraries which are included as predefined...

4 minutes read.