How To Use Clrscr In Dev C%2b%2b

Posted on  by 

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++.

  1. How To Use Clrscr In Dev C 2b 2b 3
  2. How To Use Clrscr In Dev C 2b 2b 1b
  3. How To Use Clrscr In Dev C 2b 2b 4

Re: How to declare clrscr?? Of course that in VC there is no such header, but also there is no use to call clrsr in Visual enviroment! The guy used to work on borland. The clrscr was supported in Turbo-C, and could be used including conio.h. But it is no longer part of any of the modern C C library. None of DevC, VS2008 or VS2010 have clrscr.

getch() in C++

How To Use Clrscr In Dev C%2b%2b

How To Use Clrscr In Dev C 2b 2b 3

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.

How To Use Clrscr In Dev C 2b 2b 1b

The output of the above code is:

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.

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.

The output of this program is:

How To Use Clrscr In Dev C 2b 2b 4

Note: I have given t as an input here.

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.

Output:

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

Output:
For the first 3 seconds:

After 3 seconds,

Leave a Reply

You must be logged in to post a comment.

  • Related Questions & Answers
  • Selected Reading
CC++Server Side Programming

There are several methods to clear the console or output screen and one of them is clrscr() function. It clears the screen as function invokes. It is declared in “conio.h” header file. There are some other methods too like system(“cls”) and system(“clear”) and these are declared in “stdlib.h” header file.

Here is the syntax to clear the console in C language,

Here is an example to clear the console in C language,

Let’s say we have “new.txt” file with the following content −

Now, let us see the example.

Example

Output

In the above program, we have a text file “new.txt”. A file pointer is used to open and read the file. It is displaying the content of file. To clear the console, clrscr() is used.

Coments are closed