FAQ > Clear the screen?


Match word(s).

If you have any questions or comments,
please visit us on the Forums.

FAQ > How do I... (Level 1) > Clear the screen?

This item was added on: 2002/09/13

Clearing the screen might seem like a simple thing to do, but answering the question of how to do it isn't. The main problem is that neither standard C nor standard C++ provide any reliable means for clearing the screen. The languages are designed to be platform independant, and platform independence includes the possibility that a "screen" may not even exist. Any interaction with the screen in this manner will impact the portability of your program, so it's important that you keep this in mind when developing. Also keep in mind that clearing the screen in a console mode program may interact in unwanted ways with the output of other programs. Many users do not want their programs to clear the screen for them, especially when their shell supports an easy way to do it themselves.

The best method to clear the screen may be for you to review your compilers documentation and find a suitable non-standard function (such as clrscr from conio.h). The following options will give you the results you desire as quick fixes.

Here are some options that may be open to you.


OPTION 1

Write newline characters until everything has scrolled off of the screen:

#include <stdio.h>

#define SCREEN_HEIGHT 25

int main ( void )
{
  int i;
  
  for ( i = 0; i < SCREEN_HEIGHT; i++ )
    putchar ( '\n' );
    
  return 0;
}

Advantages:
  • It doesn't rely on non-portable features or libraries.

Disadvantages:

  • You can't portably determine the number of newlines to write.

  • The cursor is placed at the bottom of the screen instead of the top.

OPTION 2

Use a text-mode library that supports the feature you want, such as Curses.

Advantages:

  • Portable, provided that you have the library.

Disadvantages:

  • You have to install a third party library or rely on the system having it.

  • There may be a lot of overhead for using the library when all you want is the one little feature.

OPTION 3

Write directly to video memory, clearing all the bits.

Advantages:

  • It works.

Disadvantages:

  • Not portable in the slightest.

  • Modern operating systems don't allow direct memory writes.

OPTION 4

Throw the monitor out the window

Advantages:


  • Pretty fail-proof.

  • Releases your anger.


Disadvantages:

  • No more monitor.

  • It makes a mess.

  • Not portable (monitors are heavy).

OPTION 5

Use the system function to call your OS's command line screen clearing program. This simulates what the user would do if you weren't so interested in clearing the screen.

#include <stdlib.h>

int main ( void )
{
  system ( "cls" ); /* Or system ( "clear" ); for POSIX */

  return 0;
}

Advantages:

  • It's very easy.

  • The system function is always available.


Disadvantages:

  • There's lots of overhead.

  • The system function is incredibly slow (not good for screen drawing).

  • There are serious security concerns (what if a maalicious user replaces the clear program?).

  • The argument to the system function is not portable.

OPTION 6 (Credit: Sunlight)

Define your own function using a platform-dependent method. For example, here is a function to clear the screen in Windows:

#include <windows.h>

void clear_screen ( void )
{
  DWORD n;                         /* Number of characters written */
  DWORD size;                      /* number of visible characters */
  COORD coord = {0};               /* Top left screen position */
  CONSOLE_SCREEN_BUFFER_INFO csbi;

  /* Get a handle to the console */
  HANDLE h = GetStdHandle ( STD_OUTPUT_HANDLE );

  GetConsoleScreenBufferInfo ( h, &csbi );

  /* Find the number of characters to overwrite */
  size = csbi.dwSize.X * csbi.dwSize.Y;

  /* Overwrite the screen buffer with whitespace */
  FillConsoleOutputCharacter ( h, TEXT ( ' ' ), size, coord, &n );
  GetConsoleScreenBufferInfo ( h, &csbi );
  FillConsoleOutputAttribute ( h, csbi.wAttributes, size, coord, &n );

  /* Reset the cursor to the top left position */
  SetConsoleCursorPosition ( h, coord );
}

Script provided by SmartCGIs