Clearing terminal in Linux with C++ code

Active3 hr before
Viewed126 times

7 Answers


Let us see the code to get the better idea.,How to print a line on the console using C#?,How to clear console in C?,How to clear console in MongoDB?


#include <iostream>
using namespace std;
int main () {
   cout << "Now the screen will be cleared!" << endl;
   cout << "Empty Screen" << endl;
   return 0;
load more v

Okay, I have been researching on how to do this, but say I am running a program that has a whole bit of output on the terminal, how would I clear the screen from within my program so that I can keep my program running?,For portability you should get the string from termcap's cl (clear) capability (Clear screen and cursor home). (Or use std::system("clear") as told by Roger Pate).,Though generally system() is to be avoided, for a user-interactive program neither the extra shell parsing nor process overhead is significant. There's no problem with shell escaping either, in this case.,You could always fork/exec to call clear if you did want to avoid system(). If you're already using [n]curses or another terminal library, use that.

Instead of depending on specific escape sequences that may break in unexpected situations (though accepting that trade-off is fine, if it's what you want), you can just do the same thing you'd do at your shell:

load more v

POSIX (Unix, Linux, Mac OSX, etc) , OS Specific ways Windows API POSIX (Unix, Linux, Mac OSX, etc) Other systems, like DOS ,Unix/Linux programming, Addenda ANSI Escape Codes Wait, how do I use this stuff?

load more v

Search Answer Titles

#include <Windows.h>

int main() {
	//print stuff
	system("cls"); // clear console
load more v

#include <stdio.h>

// f fullscreen
// r replace line

int main(){
   printf ("\f");
   printf ("asdf\n");
   return 0;

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”). ,By Using system(“clear”),Writing code in comment? Please use, generate link and share the link here.,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. 

load more v

Clear Output Screen - When we run a program, previous output or other command prompt/ Linux Terminal command's output appear there. We can clear the output screen using C program.,system() is a library function of stdlib.h header file. This function is used to run system/ Linux Terminal commands and here clear is a command to clear the output screen.,system() is a library function of stdlib.h header file. This function is used to run system/ command prompt commands and here cls is a command to clear the output screen.,Consider the following program

Consider the following program

#include <stdio.h>
#include <conio.h> /*for clrscr()*/
int main()
    int num=100;
    /*Use after declaration section*/
    clrscr(); /*clear output screen*/
    printf("value of num: %d\n",num);
    return 0;
load more v

Other "clearing-linux" queries related to "Clearing terminal in Linux with C++ code"