Open In App

How to clear console in C language?

Last Updated : 11 Nov, 2022
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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: 

  1. By Using system(“clear”)
  2. By using a regex “\e[1;1H\e[2J”
  3.  by using clrscr() function
  4.  System(“cls”) 

The following are some of the factors that influence a console’s clear screen:

  1. Type of operating system. 
  2. User compiler new or old.
  3. Need of the user.

Prerequisites :

  1. Some clear screen methods are platform specific. They won’t run on other systems or compilers.
  2. 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 :

  1. Window Specific
  2. Supported by older compiler
  3. 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 :

  1. Run this code older compiler . eg. Turbo C 
  2. 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 :

  1. Linux specific
  2. Supported by all modern compilers.
  3. 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 :

  1. 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 :
 

  1. window-specific method.
  2. supported by modern compilers.
  3. 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 :

  1. 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 :

  1. Not platform specific
  2. 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 :

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



Next Article

Similar Reads

three90RightbarBannerImg
  翻译: