Microsoft small business knowledge base

Article ID: 99261 - Last Review: November 21, 2006 - Revision: 3.3

This article was previously published under Q99261

On This Page


Some non-Microsoft versions of C++ provide a clrscr function for clearing the screen in a DOS application. However, there is no Win32 Application Programming Interface (API) or C-Runtime function that will perform this function.

To accomplish this task for a Win32 console application, use one of the following methods:
  • Use a system function.
  • Write a function that will programmatically clear the screen.
These methods are described in the following "More Information" section.


Use a system function

#include <stdlib.h>

void main()

Write a function that will programmatically clear the screen

The following function clears the screen:
 /* Standard error macro for reporting API errors */ 
 #define PERR(bSuccess, api){if(!(bSuccess)) printf("%s:Error %d from %s \ 
    on line %d\n", __FILE__, GetLastError(), api, __LINE__);}

 void cls( HANDLE hConsole )
    COORD coordScreen = { 0, 0 };    /* here's where we'll home the
                                        cursor */ 
    BOOL bSuccess;
    DWORD cCharsWritten;
    CONSOLE_SCREEN_BUFFER_INFO csbi; /* to get buffer info */ 
    DWORD dwConSize;                 /* number of character cells in
                                        the current buffer */ 

    /* get the number of character cells in the current buffer */ 

    bSuccess = GetConsoleScreenBufferInfo( hConsole, &csbi );
    PERR( bSuccess, "GetConsoleScreenBufferInfo" );
    dwConSize = csbi.dwSize.X * csbi.dwSize.Y;

    /* fill the entire screen with blanks */ 

    bSuccess = FillConsoleOutputCharacter( hConsole, (TCHAR) ' ',
       dwConSize, coordScreen, &cCharsWritten );
    PERR( bSuccess, "FillConsoleOutputCharacter" );

    /* get the current text attribute */ 

    bSuccess = GetConsoleScreenBufferInfo( hConsole, &csbi );
    PERR( bSuccess, "ConsoleScreenBufferInfo" );

    /* now set the buffer's attributes accordingly */ 

    bSuccess = FillConsoleOutputAttribute( hConsole, csbi.wAttributes,
       dwConSize, coordScreen, &cCharsWritten );
    PERR( bSuccess, "FillConsoleOutputAttribute" );

    /* put the cursor at (0, 0) */ 

    bSuccess = SetConsoleCursorPosition( hConsole, coordScreen );
    PERR( bSuccess, "SetConsoleCursorPosition" );

  • Microsoft Win32 Application Programming Interface, when used with:
    • Microsoft Windows NT 3.51 Service Pack 5
    • Microsoft Windows NT 4.0
    • Microsoft Windows 95
    • Microsoft Windows 98 Standard Edition
    • Microsoft Windows 2000 Standard Edition
    • Microsoft Windows Millennium Edition
    • the operating system: Microsoft Windows XP
kbhowto kbapi kbconsole kbkernbase kbcode KB99261
Additional support options
Ask The Microsoft Small Business Support Community
Contact Microsoft Small Business Support
Find Microsoft Small Business Support Certified Partner
Find a Microsoft Store For In-Person Small Business Support