Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you clear the console screen in C?

Is there a "proper" way to clear the console window in C, besides using system("cls")?

like image 722
devurs Avatar asked Feb 27 '10 15:02

devurs


People also ask

How do I clear the console in C?

A file pointer is used to open and read the file. It is displaying the content of file. To clear the console, clrscr() is used.

How do I clear the console screen?

You can use Ctrl+L keyboard shortcut in Linux to clear the screen. It works in most terminal emulators. If you use Ctrl+L and clear command in GNOME terminal (default in Ubuntu), you'll notice the difference between their impact.


1 Answers

printf("\e[1;1H\e[2J"); 

This function will work on ANSI terminals, demands POSIX. I assume there is a version that might also work on window's console, since it also supports ANSI escape sequences.

#include <unistd.h>  void clearScreen() {   const char *CLEAR_SCREEN_ANSI = "\e[1;1H\e[2J";   write(STDOUT_FILENO, CLEAR_SCREEN_ANSI, 12); } 

There are some other alternatives, some of which don't move the cursor to {1,1}.

like image 87
Avinash Katiyar Avatar answered Sep 19 '22 20:09

Avinash Katiyar