ANSI.SYS Escape Codes with printf() for Screen Control

Last reviewed: July 17, 1997
Article ID: Q68874
6.00 6.00a 6.00ax 7.00 | 6.00 6.00a | 1.00 1.50
MS-DOS                 | OS/2       | WINDOWS
kbprg kbcode

The information in this article applies to:

  • The C Run-time (CRT), included with:

        - Microsoft C for MS-DOS, versions 6.0, 6.0a, and 6.0ax
        - Microsoft C for OS/2, versions 5.1, 6.0, and 6.0a
        - Microsoft C/C++ for MS-DOS, version 7.0
        - Microsoft Visual C++ for Windows, versions 1.0 and 1.5
    

SUMMARY

The MS-DOS ANSI.SYS driver may be used to perform screen control functions in printf() output. Setting screen attributes, positioning the cursor to a row and column, and clearing the screen are some examples of functions that may be done with this driver using the correct escape sequences.

MORE INFORMATION

The ANSI.SYS driver must be loaded from CONFIG.SYS in order for these functions to work correctly. All of the sequences start with the escape code "\33" (representing the value for escape in octal), followed by the appropriate set of characters needed to perform the desired function. The sample code below demonstrates some of these functions.

This topic is discussed in detail on pages 224-225 of the Microsoft Press book "Variations in C." Another good source of information on ANSI.SYS and the ANSI escape sequences is "The MS-DOS Encyclopedia" (on pages 731-738) or in most MS-DOS manuals under the PROMPT command.

In OS/2, the ANSI driver is enabled by default for all sessions except the Presentation Manager. To enable ANSI support in the DOS box, a line must be added to the CONFIG.SYS file. See your OS/2 documentation for more information on the exact syntax.

Sample Code

/* Compile options needed: none
*/

#include <stdio.h>

void main( void)
{
   int row = 10;
   int col = 20;
   int num = 1;

   printf( "\33[2J");                // clears the screen

   printf( "\33[%d;%dH", row, col);  // positions the cursor at
                                     // row 10, column 20

   printf( "\33[%dA", num);          // moves the cursor up one line

   printf( "\33[%dB", num);          // moves the cursor down one line

   printf( "\33[7m");                // sets the attribute to reverse
                                     // video
}


Additional reference words: kbinf 5.10 6.00 6.00a 6.00ax 7.00 1.00 1.50
KBCategory: kbprg kbcode
KBSubcategory: CRTIss
Keywords : kb16bitonly


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: July 17, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.