![]() |
VOOZH | about |
It is one of the basic need a program may required i.e clear the console during execution time.
A console screen must be cleared once its stated objectives have been completed. A user also needs a clear screen for new data. Like any other language, C offers a variety of ways to clear the console screen.
The clear screen method or function not only depends on types of operating systems but also compilers, which include modern as well as older compilers
There is function named clrscr() which is included in conio.h and is a nonstandard function and is present in conio.h header file which is mostly used by MS-DOS compilers like Turbo C. It is not part of the C standard library or ISO C, nor is it defined by POSIX.
So what should we use there?
There are two more ways to clear console:
The following are some of the factors that influence a console's clear screen:
clrscr() is an abbreviation of the clear screen. It aims to clear the console screen. clrscr() is a library function located in the console input output header file <conio.h>. The previous screen on the console is cleared whenever the clrscr () is invoked in a program. To call the clrscr() function, it must define inside any function or the main function.
void (void clrscr);
This function doesn’t accept any parameters.
This function doesn’t return any value.
let's take an example that will display the addition of two numbers using clrscr().
Enter No 1 and No 2: 5 9 Sum Of Two Number is = 14
Here, we've performed a number addition calculation and shown the results on the console screen. The console screen is cleared by clrscr() and ready for new data when the user runs this code again.
If the user don't use clrscr() or any other function to clear the screen, your output will look like this.
Enter No 1 and No 2: 5 9 Sum Of Two Numbers is = 14 Enter No 1 and No 2: 6 10 Sum Of Two Numbers is = 16
like clrscr() function system("clear") also used to clear console screen in C. This function can be found in the stdlib.h header file. This method is useful in Linux operating system to clear the console screen. whenever the system(clear) function invokes the program it calls the system shell then a clear operation gets performed.
It is supported by almost all online compilers and GCC compilers whereas clrscr() won't support it.
System("clear")This function doesn’t accept any parameters.
This function doesn’t return any value.
Let's take an example that display welcome message.
Welcome To Geeksforgeeks A computer science portal
Here, we've displayed two welcome message . we also used getchar() method accept string which make user to press enter key . when user press enter key previous screen get cleared and displayed "A computer science portal" message.
like clrscr() ,system("clear") we use system.cls() function to clear console screen . This function is present in the standard library header file <stdlib.h.> .whenever system(cls) function invoke in the program it calls system shell then clear operation gets performed. It is supported by all online compiler and GCC compilers.
system.cls()
This function doesn’t accept any parameters.
This function doesn’t return any value.
let's take an example that will display the welcome message using system.cls().
Welcome To Geeksforgeeks A computer science portal
Here, we've displayed two welcome message .To clear screen we have used system("cls") which make clear previous screen.
In C, \e[1;1H\e[2J regex is used to clear the console screen just like any other method or function.
Where
/e provides an escape. and e [1;1H] place your cursor in the upper right corner of the console screen. and e [2J adds a space to the top of all existing screen characters.
Whenever regex is invoked in a program, it clears the previous console screen and makes the system available for new data.
printf("\e[1;1H\e[2J");This function doesn’t accept any parameters.
This function doesn’t return any value.
Welcome To Geeksforgeeks A computer science portal
Here, we've displayed two welcome message .To clear screen we have used system regex which make clear previous screen.
Now question arises which should we use and why:
Using regex is a better way.The reason is its faster execution. By using regex we can perform clear screen operation very fastly in comparison to using system("clear").
Below c program will demonstrate how fast regex is then the system("clear")
The system("clear") is included in stdlib.h and also work only in linux system to use this in window use system("cls").
Output:
geeks for geeks 9999
Time taken by system("clear") 0.934388
Time taken by regex 0.000001
NOTE:The output time may differ but the difference in both time will always be large.And also run this program only in your system's console not here.
In real world problem , clear console method make system more readable and efficient to use.