The C standard library does not provide a way to clear the screen. To do this, you need a library that depends on the operating system.
On DOS and Windows, for a program running in the DOS or Windows console, you can use the DOS / Windows extensions provided in the main C library supplied with the OS:
#include <conio.h> clrscr();
On Unix systems, you can use the curses library, which is provided with the OS. Curse library ports exist for most operating systems, including Windows, so this is the way to the portable program. Associate your program with -lcurses and use
#include <curses.h> erase();
Some terminals and terminal emulators perform special functions, such as clearing the screen when they receive an escape sequence. Most terminals comply with the ANSI standard, which defines several escape sequences; "\x1b[2J" is such a sequence, and its effect is to clear the screen. Pay attention to capital J On such a terminal, fputs("\x1b[2J", stdout) clears the screen. This is what the curses library does when you call erase() on such a terminal; The curses library includes a database of terminal types and which escape sequences to use on different types.
Gilles
source share