getch() and clrscr() functions in C++

In this post, we will learn about getch() and clrscr() functions in C++. These functions are defined in non-standard “conio.h” header file which stands for Console Input/Output. This header file contains functions for console input/output and mostly used in turbo C++.

getch() in C++

getch() is a predefined non-standard function in “conio.h” header. It is used to tell the compiler to wait until the user enters a character. This is often used at the end of the main function (before the return statement) so that we can see the output. If we don’t use getch() at the end, the program is executed but we cannot see the output.

See the example code.

#include <iostream.h>
#include <conio.h>

int main()
{
    cout<<"This is the output."<<endl;
    getch();
    return 0;
}

The output of the above code is:

This is the output.

After the output is shown,  getch() waits for the user to enter any character. Hence the user can view the output on the console until he presses any key on the keyboard.

Now if we remove getch()  statement from the example code, the program is compiled successfully. But the console turns off as soon as the program is executed completely which is, in general, a matter of milliseconds.

Let’s understand this using delay() function of C++. If you don’t know about the delay() function, see this : Delay() function in C++

In the below example we are printing a statement and then creating a 3-second delay. See how it works.

#include <iostream.h>
#include <conio.h>
#include <dos.h>  //for delay()

int main()
{
    cout<<"This is the output."<<endl;
    delay(3000);
    return 0;
}

Output:

This is the output.

You can see that after printing the output, the compiler waits for 3 seconds before the console turns off.

Also, see this to understand getch() better.

#include <iostream.h>
#include <conio.h>

int main()
{
     char a;
     cout<<"Enter a character."<<endl;
     a=getch();     // I havve given t as input
     cout<<"Entered character is "<< a << "." <<endl;
     getch();             
     return 0;
}

The output of this program is:

Note: I have given t as an input here.

Enter a character.
Entered character is t.

Here, as you can see getch() stores the value of given character in variable a. The last getch() has been used to view the output.

clrscr() in C++

clrscr() function is also a non-standard function defined in “conio.h” header. This function is used to clear the console screen. It is often used at the beginning of the program (mostly after variable declaration but not necessarily) so that the console is clear for our output.

See the code here.

#include <iostream.h>
#include <conio.h>

int main()
{
    int a=1;
    clrscr();
    cout<<"a is " << a << endl;
    getch();
    return 0;
}

Output:

a is 1

The below code will help you understand the functioning of clrscr().

#include <iostream.h> 
#include <conio.h> 
#include <dos.h>

int main() 
{
    int a=1; 
    cout<<"Before clrscr() statement."<<endl;
    delay(3000);
    clrscr();   
    cout<<"a is " << a << endl; 
    getch();  
    return 0; 
}

Output:
For the first 3 seconds:

Before clrscr() statement.

After 3 seconds,

a is 1

Leave a Reply

Your email address will not be published.