Is there a standard way to clear the screen in C/C++?

Question:

Hi, I'm new to programming in general; I would like to know if there is a standard way to clean the screen. I know you can use system(cls) on windows or system(clear) on linux. But is there a way to do it regardless of the operating system? be it Windows, FreeBSD, Linux (also Android)

Answer:

One of the shortest answers I've ever written: no, there isn't .

Neither C nor C++ has anything like the concept of pantalla . They have input/output streams , which are abstract things not associated with any kind of physical device. They may or may not be associated with a terminal .

What there are are more or less portable libraries for handling terminals . A terminal is a software/hardware entity that interprets certain character sequences in a certain way. On Linux, in text mode, it is of type linux . In graphical mode, this is usually xterm or xterm-color .

On Windows, I think the ventana MS-DOS has support, among others, for ANSI terminals and some more.

Always remember that this is not safe: if I launch your program like this:

yourProgram > myfile.txt

What type is that terminal? It's just a file, and any sequence of control codes you send will be saved as is, of course, without having any effect on the screen .

Searching in google portable terminal lib some accounts come out. You can take a look at them at will 🙂

Another point of view is to deal directly with the functions of the terminal program you are using: the Windows console, the preferred terminal emulator on Linux. So yes you can interact directly with the screen , regardless of possible input/output redirections… but that's even less portable: now we depend exactly on the API of the chosen program.

Scroll to Top