Clear screen on Mac. Language C [closed]

0

I was wondering if the system(“clear”); function works on Mac or, if not, is there a different one for this operating system? Since I only have Linux to try! Thanks!

    
asked by Alexa Garcia 21.05.2018 в 00:34
source

2 answers

2

Apart from that, as it explains efferion, there is no standard way to erase the screen from C, what does exist is a standard of escape codes (ANSI X3.64) to which many terminals adhere, and that would allow delete it easily from any programming language.

Following this standard, if the terminal receives certain sequences of characters, it will perform certain actions. In particular, if you receive the sequence of codes (I write them in hexadecimal) 1b , 5b , 32 , 4a , will erase the screen.

These codes represent, respectively, the ASCII code of the "escape", the ascii code of the symbol [ , the ASCII code of the digit 2 and the ascii code of the letter J .

From a C program you can send that sequence of codes to the standard output by:

printf("3[2J");

The 3 is a way of entering any non-printable ASCII code into a string, writing it in octal (base 8). In that base 33 equals the 1b hexadecimal.

If the standard output of that program is connected to a terminal compatible with the ANSI standard mentioned above, the screen will be cleared. You have more possible escape codes on Wikipedia

Which terminals are compatible?

  • The linux console
  • Linux terminal emulators (xterm, and others)
  • The application Terminal of OSX (mac)
  • Many other terminal emulators for OSX, such as iterm
  • Applications for remote access via ssh (such as putty for windows, and remote connection applications for Android such as JuiceSSH or Termius for iOS, among many others)

Not compatible:

  • The windows terminal ( cmd.exe )

    Although it seems to be that since the Windows 10 Anniversary Update, if it supports it, but not by default, it should be enabled and I'm not sure how.

In summary, I would dare to say that the trick would work basically everywhere except Windows .

And of course, for Linux it is a more efficient alternative than system("clear") , since what system() does is create a new process ( fork() ), load inside it a command interpreter ( bash or the one you use by default), and ask him to execute "clear" . And for that, the newly loaded command interpreter will have to search in PATH for an executable called clear , and load it into memory, for which it needs to do another process ( fork() ) in which it loads the executable clear , and wait for it to end, after which the shell in turn ends and system() returns. As you see, many steps, and everything so that in the end, most likely, the command clear is limited to send to the terminal "3[2J" .

    
answered by 21.05.2018 в 11:17
1

In C there is no standard way to clear the screen.

The example you propose, that is, using system("clear") could only work on systems that have an application called clear whose role is to clean the console.

But, as I say, the C standard does not include any mechanism to perform this task, then to clean the screen you only have to resort to functions of the operating system you are working on, which results in a solution not portable, or resort to third-party libraries (if possible multiplatform) that incorporate this functionality ... in this case your application will be linked to this library and that is not always an option (for example in class duties). / p>

In this particular case there is no good answer, I'm sorry.

    
answered by 21.05.2018 в 09:14