How do you clear the console screen in C?

devurs picture devurs · Feb 27, 2010 · Viewed 389.3k times · Source

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

Answer

Avinash Katiyar picture Avinash Katiyar · Oct 5, 2011
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}.