1

Recently I’ve discovered a way to clear the terminal in the compiler I’m using, with system("clear"), which doesn’t need any additional header files from the looks of it. I was wondering if this function is both compatible with a multitude of popular compilers out there, and is it stable (will I run into problems if I use it multiple times in one project)?

Another way I tried to clear the terminal was through an escape character, but after doing some research it seems unreliable because it’s specific to ANSI compliant terminals. I was wondering if system("clear") had that kind of requirement as well, because I want to make sure I can use it a good amount in a project I’m working on.

Govind Parmar
  • 20,656
  • 7
  • 53
  • 85
Selisine
  • 28
  • 6
  • 2
    Yes, there is a requirement: your system has `clear` command that clears the terminal. – MikeCAT Mar 13 '23 at 12:47
  • 4
    The `system` part is widely compatible. The `clear` part is not. There are terminal libraries, like **ncurses**, which broadly support many terminal types (both ANSI compliant and non-ANSI). – Eljay Mar 13 '23 at 12:48
  • 3
    What do you mean by "stable"? The [`std::system`](https://en.cppreference.com/w/cpp/utility/program/system) function is in the standard library. The commands you can invoke are not, and they do very much differ between systems. – Some programmer dude Mar 13 '23 at 12:48
  • 2
    In general, C++ doesn't really specify anything about the "terminal". All terminal-handling is going to be platform- and system-specific. There are systems (typically embedded systems) which doesn't have a command-line terminal at all, and where the `std::system` function makes no sense. Or perhaps the user of your program have redirected output to a file, would a `clear` command make sense then? – Some programmer dude Mar 13 '23 at 12:49
  • 4
    [Why should the `system()` function be avoided in C and C++?](https://stackoverflow.com/q/19913446/995714), [Are `system()` calls evil?](https://stackoverflow.com/q/8086071/995714), [Are ALL `system()` calls a security risk in c++?](https://stackoverflow.com/q/48069989/995714) – phuclv Mar 13 '23 at 12:58
  • 1
    `system()` is a security nightmare and should (IMHO) *never* be used. Also, `clear` won't work on Windows or other OS's that don't have it. – Jesper Juhl Mar 13 '23 at 13:08
  • 1
    The `system()` function is stable, in the sense that the C++ standard specifies how it is declared and which standard headers it is declared in. However, the behaviour of that function is implementation defined - for example, it is unspecified how the string is interpreted (e.g. characteristics of the command line interpreter or shell it is passed to). There is no requirement that `system("clear")` will clear a terminal (in practice, it clears a terminal on some systems but not others). – Peter Mar 13 '23 at 13:09

1 Answers1

3

I was wondering if this function is both compatible with a multitude of popular compilers

Compilers compile code. system("clear") is compatible with the compiler.

will I run into problems if I use it multiple times in one project)?

You will run into the exact same problems when you use it once. I would expect it to be "safe" to use multiple times, it just clears the terminal.

I was wondering if system("clear") had that kind of requirement as well

There are multiple requirements for system:

  • the standard library has to provide system() function.
  • the system has to have a shell
  • that shell can be executed

These are just not true on any microcontroller.

There are requirements for a shell to have a clear command, that command has to exist. This is not true on small operating systems or on any Linux systems without installed ncurses, etc.

Finally, the command clear has to do the right thing. This means you have to have something-compliant terminal that clear knows a way to communicate here. This implies multiple levels - on Linux, there is a whole database that solely contains what to send on which terminal to do what. On Linux from ncurses clear queries that whole database, finds terminal configuration depending on $TERM environment variable, finds the sting to send to clear the string for that terminal, and finally sends it. On any other system, this may work anyhow differently.

Instead, consider just using ncurses in your project if you depend on terminal capabilities.

KamilCuk
  • 120,984
  • 8
  • 59
  • 111
  • 2
    not all Linux distros have `clear`, so even on Linux it's not completely "stable" – phuclv Mar 13 '23 at 13:06
  • I was not aware that ncurses was a thing up until now, so I’ll give that a shot. The project I’m making relies pretty heavily on terminal right now, at least until I can stick and convert it through a game engine. Thanks! – Selisine Mar 13 '23 at 13:07