How to clear console in C language?
Last Updated :
11 Nov, 2022
It is one of the basic need a program may required i.e clear the console during execution time.
Need of a clear console screen :
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:
- By Using system("clear")
- By using a regex "\e[1;1H\e[2J"
- by using clrscr() function
- System(“cls”)
The following are some of the factors that influence a console's clear screen:
- Type of operating system.
- User compiler new or old.
- Need of the user.
Prerequisites :
- Some clear screen methods are platform specific. They won't run on other systems or compilers.
- If you are using a visual studio then prefer the system.("cls")
clrscr()
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.
Properties :
- Window Specific
- Supported by older compiler
- Doesn't allocate extra resource.
Syntax
void (void clrscr);
Parameter:
This function doesn’t accept any parameters.
Return :
This function doesn’t return any value.
Example :
let's take an example that will display the addition of two numbers using clrscr().
Prerequisites :
- Run this code older compiler . eg. Turbo C
- If you run this code on a modern compiler , it will show an error . As they don't include <conio.h> header file.
Program Code :
C
// program to clear console screen in C using clrscr() function.
#include <conio.h>
#include <stdio.h>
// driver code
int main()
{
int number1, number2, addition;
// clear screen
clrscr();
// input number1 and number2
printf("Enter No 1 and No 2\n");
scanf("%d%d", &number1, &number2);
addition = number1 + number2;
printf("Sum Of Two Number is =%d", addition);
}
Output :
Enter No 1 and No 2:
5 9
Sum Of Two Number is = 14
Explanation :
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.
what if we clear screen ?
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
system("clear")
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.
Properties :
- Linux specific
- Supported by all modern compilers.
- Make the software more readable.
Syntax :
System("clear")
Parameter :
This function doesn’t accept any parameters.
Return Type :
This function doesn’t return any value.
Program Code :
Let's take an example that display welcome message.
Prerequisites :
- Run this code online compiler . you can find online compiler on google easily.
C
//Program to clear screen using system("clear");
#include<stdio.h>
#include<stdlib.h>
int main()
{
printf("Welcome To Geeksforgeeks\n");
getchar();
system("clear");
printf("A computer science portal ");
}
Output :
Welcome To Geeksforgeeks
A computer science portal
Explanation :
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.
system.cls()
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.
Properties :
- window-specific method.
- supported by modern compilers.
- supported by older compilers like Turbo C.
Syntax :
system.cls()
Parameter :
This function doesn’t accept any parameters.
Return Type :
This function doesn’t return any value.
Program Code :
let's take an example that will display the welcome message using system.cls().
Prerequisites :
- Run this code visual studio .
C
//clear console screen in c using cls method
#include<stdio.h>
#include<stdlib.h>
int main()
{
printf("Welcome To Geeksforgeeks");
getchar();
system("cls");
printf("A computer science portal ");
}
output :
Welcome To Geeksforgeeks
A computer science portal
Explanation :
Here, we've displayed two welcome message .To clear screen we have used system("cls") which make clear previous screen.
Using \e[1;1H\e[2J Regex :
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.
Properties :
- Not platform specific
- Easy to remember and understand
Syntax :
printf("\e[1;1H\e[2J");
Parameter :
This function doesn’t accept any parameters.
Return Type :
This function doesn’t return any value.
Prerequisites :
- Run this code visual studio.
Program Code :
C
//clear console screen in c using [\e[1;1H\e[2J] method
#include<stdio.h>
#include<stdlib.h>
int main()
{
printf("Geeksforgeeks\n");
printf("\e[1;1H\e[2J");
printf("A computer Science portal");
}
output :
Welcome To Geeksforgeeks
A computer science portal
Explanation :
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").
C
// C program for clearing console and
// comparing two different methods
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main()
{
int i = 0;
double time_taken;
clock_t t1, t2;
// a loop for showing geeks for geeks
// repeating by clearing console using
// system("clear")
// Noting start time
t1 = clock();
for (i; i < 10000; i++)
{
system("clear");
printf("geeks for geeks %d\n", i);
}
// Calculating total time taken by
// system("clear")
t1 = clock() - t1;
i = 0;
// Noting start time of regex
t2 = clock();
for (i; i < 10000; i++)
{
printf("\e[1;1H\e[2J");
printf("geeks for geeks %d\n", i);
}
// calculating total time taken by regex
t2 = clock() - t2;
// printing taken by both
printf("Time taken by system\(\"clear\") %f\n",
((double)t1) / CLOCKS_PER_SEC);
printf("Time taken regex %f",
((double)t2) / CLOCKS_PER_SEC);
return 0;
}
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.
Conclusion :
In real world problem , clear console method make system more readable and efficient to use.
Similar Reads
How To Compile And Run a C/C++ Code In Linux
C Programming Language is mainly developed as a system programming language to write kernels or write an operating system. C++ Programming Language is used to develop games, desktop apps, operating systems, browsers, and so on because of its performance. In this article, we will be compiling and exe
4 min read
multiset clear() function in C++ STL
The multiset::clear() function is a built-in function in C++ STL which removes all elements from the multiset container. The final size of multiset container after removal is 0. Syntax: multiset_name.clear() Parameters: The function does not accept any parameter. Return Value: The function does not
2 min read
kbhit in C language
kbhit() functionality is basically stand for the Keyboard Hit. This function is deals with keyboard pressing kbhit() is present in conio.h and used to determine if a key has been pressed or not. To use kbhit function in your program you should include the header file "conio.h". If a key has been pre
2 min read
How to Hide the Console Window of a C Program?
The task is to hide the console window of a C program. The program for the same is given below. Note: The results of the following program can only be seen when it is executed on a console. C // C program to hide the console window #include <stdio.h> #include <windows.h> int main() { HW
2 min read
map clear() in C++ STL
The map clear() function in C++ is used to remove all elements from the map container and make its size to 0. In this article, we will learn about the map clear() function in C++.Letâs take a quick look at a simple example that uses map clear() method:C++#include <bits/stdc++.h> using namespac
2 min read
set::clear in C++ STL
Sets are a type of associative containers in which each element has to be unique, because the value of the element identifies it. The value of the element cannot be modified once it is added to the set, though it is possible to remove and add the modified value of that element. set::clear() clear()
2 min read
unordered_set clear() function in C++ STL
The unordered_set::clear() function is a built-in function in C++ STL which is used to clear an unordered_set container. That is, this function removes all of the elements from an unordered_set and empties it. All of the iterators, pointers, and references to the container are invalidated. This redu
2 min read
How to Empty a Char Array in C?
Prerequisite: Char Array in C Char arrays are used for storing multiple character elements in a continuous memory allocated. In a few questions, we have the necessity to empty the char array but You canât clear a char array by setting the individual members to some value indicating that they donât c
4 min read
list::clear() in C++ STL
Lists are containers used in C++ to store data in a non contiguous fashion, Normally, Arrays and Vectors are contiguous in nature, therefore the insertion and deletion operations are costlier as compared to the insertion and deletion option in Lists. list::clear()clear() function is used to remove a
1 min read
C Language | Set 5
Following questions have been asked in GATE CS 2008 exam. 1. What is printed by the following C program? C int f(int x, int *py, int **ppz) { int y, z; **ppz += 1; z = **ppz; *py += 2; y = *py; x += 3; return x + y + z; } void main() { int c, *b, **a; c = 4; b = &c; a = &b; printf( "%d
4 min read