View Full Version : function to clear the screen in C language

April 29th, 2010, 01:20 AM
This thread is for the one who knows C Programming under windows and Linux well.can anyone please tell me the function to clear the console in linux using C language.It is done under windows by using clrscr() by including <conio.h>.Also,please tell me the header file to use and the respective function.

April 29th, 2010, 02:19 AM
Assuming you are using linux:


April 29th, 2010, 02:34 AM
But which header file to use this function?

April 29th, 2010, 02:37 AM
#include <stdio.h>
#include <stdlib.h>

April 29th, 2010, 11:36 AM
When it comes to taking complete control of the console you'll want to use the ncurses library.

Keeping things simple:

Escape sequence example:

printf("\x1b[s\x1b[2J\x1b[10;25HTaking control of your console.\x1b[u");

April 29th, 2010, 12:32 PM
1. use ncurses
2. escape chars are evil
3. system() is evil

April 29th, 2010, 02:47 PM
1. use ncurses
2. escape chars are evil
3. system() is evil


ncurses is the standard way to go not only on GNU/Linux but also in all the rest of UNIX and Unix-like OSs. It's a weird library in some extents, but is much nicer than all other solutions.

Also, system("clear") won't work very well when running under a virtual terminal (xterm, gnome-terminal, etc.) because scrolling the text up will still show the previous contents because "clear" actually just inserts as much whitespace necessary to place the prompt back again to the first line... at console, using Shift+PgUp will also have the same effect.