Dev C++ Printf Color

Dev C++ Printf Color Average ratng: 8,6/10 924 votes

Sep 15, 2012  The game (an offshoot of Scrabble) allows tiles (letters) to be put one on top of another - up to five and it was doing fine until a friend went to Win7 (64Bit). I have rewritten it in C but cannot find (or work out) how to change the colours of the letters on the board. I used the colour coding: 1 letter displayed in white 2 letters displayed as. Console Virtual Terminal Sequences.; 26 minutes to read +4; In this article. Virtual terminal sequences are control character sequences that can control cursor movement, color/font mode, and other operations when written to the output stream. Mar 19, 2016  Customizing your editor Dev C Plus Plus code eagles. The font size and background color may no appear nice to you hence the need for customization. Using Borland graphics.h with Dev-C.

  1. C++ Printf String

Virtual terminal sequences are control character sequences that can control cursor movement, color/font mode, and other operations when written to the output stream. Sequences may also be received on the input stream in response to an output stream query information sequence or as an encoding of user input when the appropriate mode is set.

You can use GetConsoleMode and SetConsoleMode flags to configure this behavior. A sample of the suggested way to enable virtual terminal behaviors is included at the end of this document.

The behavior of the following sequences is based on the VT100 and derived terminal emulator technologies, most specifically the xterm terminal emulator. More information about terminal sequences can be found at http://vt100.net and at http://invisible-island.net/xterm/ctlseqs/ctlseqs.html.

Output Sequences

The following terminal sequences are intercepted by the console host when written into the output stream, if the ENABLE_VIRTUAL_TERMINAL_PROCESSING flag is set on the screen buffer handle using the SetConsoleMode flag. Note that the DISABLE_NEWLINE_AUTO_RETURN flag may also be useful in emulating the cursor positioning and scrolling behavior of other terminal emulators in relation to characters written to the final column in any row.

Simple Cursor Positioning

In all of the following descriptions, ESC is always the hexadecimal value 0x1B. No spaces are to be included in terminal sequences. For an example of how these sequences are used in practice, please see the example at the end of this topic.

The following table describes simple escape sequences with a single action command directly after the ESC character. These sequences have no parameters and take effect immediately.

All commands in this table are generally equivalent to calling the SetConsoleCursorPosition console API to place the cursor.

Cursor movement will be bounded by the current viewport into the buffer. Scrolling (if available) will not occur.

ESC ACUUCursor Up by 1
ESC BCUDCursor Down by 1
ESC CCUFCursor Forward (Right) by 1
ESC DCUBCursor Backward (Left) by 1
ESC MRIReverse Index – Performs the reverse operation of n, moves cursor up one line, maintains horizontal position, scrolls buffer if necessary*
ESC 7DECSCSave Cursor Position in Memory**
ESC 8DECSRRestore Cursor Position from Memory**

* If there are scroll margins set, RI inside the margins will scroll only the contents of the margins, and leave the viewport unchanged. (See Scrolling Margins)

**There will be no value saved in memory until the first use of the save command. The only way to access the saved value is with the restore command.

Cursor Positioning

The following tables encompass Control Sequence Introducer (CSI) type sequences. All CSI sequences start with ESC (0x1B) followed by [ (left bracket, 0x5B) and may contain parameters of variable length to specify more information for each operation. This will be represented by the shorthand <n>. Each table below is grouped by functionality with notes below each table explaining how the group works.

For all parameters, the following rules apply unless otherwise noted:

  • <n> represents the distance to move and is an optional parameter
  • If <n> is omitted or equals 0, it will be treated as a 1
  • <n> cannot be larger than 32,767 (maximum short value)
  • <n> cannot be negative

All commands in this section are generally equivalent to calling the SetConsoleCursorPosition console API.

Cursor movement will be bounded by the current viewport into the buffer. Scrolling (if available) will not occur.

ESC [ <n> ACUUCursor UpCursor up by <n>
ESC [ <n> BCUDCursor DownCursor down by <n>
ESC [ <n> CCUFCursor ForwardCursor forward (Right) by <n>
ESC [ <n> DCUBCursor BackwardCursor backward (Left) by <n>
ESC [ <n> ECNLCursor Next LineCursor down to beginning of <n>th line in the viewport
ESC [ <n> FCPLCursor Previous LineCursor up to beginning of <n>th line in the viewport
ESC [ <n> GCHACursor Horizontal AbsoluteCursor moves to <n>th position horizontally in the current line
ESC [ <n> dVPAVertical Line Position AbsoluteCursor moves to the <n>th position vertically in the current column
ESC [ <y> ; <x> HCUPCursor Position*Cursor moves to <x>; <y> coordinate within the viewport, where <x> is the column of the <y> line
ESC [ <y> ; <x> fHVPHorizontal Vertical Position*Cursor moves to <x>; <y> coordinate within the viewport, where <x> is the column of the <y> line
ESC [ sANSISYSSCSave Cursor – Ansi.sys emulation**With no parameters, performs a save cursor operation like DECSC
ESC [ uANSISYSSCRestore Cursor – Ansi.sys emulation**With no parameters, performs a restore cursor operation like DECRC

*<x> and <y> parameters have the same limitations as <n> above. If <x> and <y> are omitted, they will be set to 1;1.

**ANSI.sys historical documentation can be found at https://msdn.microsoft.com/library/cc722862.aspx and is implemented for convenience/compatibility.

Cursor Visibility

The following commands control the visibility of the cursor and its blinking state. The DECTCEM sequences are generally equivalent to calling SetConsoleCursorInfo console API to toggle cursor visibility.

ESC [ ? 12 hATT160Text Cursor Enable BlinkingStart the cursor blinking
ESC [ ? 12 lATT160Text Cursor Disable BlinkingStop blinking the cursor
ESC [ ? 25 hDECTCEMText Cursor Enable Mode ShowShow the cursor
ESC [ ? 25 lDECTCEMText Cursor Enable Mode HideHide the cursor

Viewport Positioning

All commands in this section are generally equivalent to calling ScrollConsoleScreenBuffer console API to move the contents of the console buffer.

Caution The command names are misleading. Scroll refers to which direction the text moves during the operation, not which way the viewport would seem to move.

ESC [ <n> SSUScroll UpScroll text up by <n>. Also known as pan down, new lines fill in from the bottom of the screen
ESC [ <n> TSDScroll DownScroll down by <n>. Also known as pan up, new lines fill in from the top of the screen

The text is moved starting with the line the cursor is on. If the cursor is on the middle row of the viewport, then scroll up would move the bottom half of the viewport, and insert blank lines at the bottom. Scroll down would move the top half of the viewport’s rows, and insert new lines at the top.

Also important to note is scroll up and down are also affected by the scrolling margins. Scroll up and down won’t affect any lines outside the scrolling margins.

The default value for <n> is 1, and the value can be optionally omitted.

Text Modification

All commands in this section are generally equivalent to calling FillConsoleOutputCharacter, FillConsoleOutputAttribute, and ScrollConsoleScreenBuffer console APIs to modify the text buffer contents.

ESC [ <n> @ICHInsert CharacterInsert <n> spaces at the current cursor position, shifting all existing text to the right. Text exiting the screen to the right is removed.
ESC [ <n> PDCHDelete CharacterDelete <n> characters at the current cursor position, shifting in space characters from the right edge of the screen.
ESC [ <n> XECHErase CharacterErase <n> characters from the current cursor position by overwriting them with a space character.
ESC [ <n> LILInsert LineInserts <n> lines into the buffer at the cursor position. The line the cursor is on, and lines below it, will be shifted downwards.
ESC [ <n> MDLDelete LineDeletes <n> lines from the buffer, starting with the row the cursor is on.

For IL and DL, only the lines in the scrolling margins (see Scrolling Margins) are affected. If no margins are set, the default margin borders are the current viewport. If lines would be shifted below the margins, they are discarded. When lines are deleted, blank lines are inserted at the bottom of the margins, lines from outside the viewport are never affected.

For each of the sequences, the default value for <n> if it is omitted is 0.

For the following commands, the parameter <n> has 3 valid values:

  • 0 erases from the current cursor position (inclusive) to the end of the line/display
  • 1 erases from the beginning of the line/display up to and including the current cursor position
  • 2 erases the entire line/display
ESC [ <n> JEDErase in DisplayReplace all text in the current viewport/screen specified by <n> with space characters
ESC [ <n> KELErase in LineReplace all text on the line with the cursor specified by <n> with space characters

Text Formatting

All commands in this section are generally equivalent to calling SetConsoleTextAttribute console APIs to adjust the formatting of all future writes to the console output text buffer.

This command is special in that the <n> position below can accept between 0 and 16 parameters separated by semicolons.

When no parameters are specified, it is treated the same as a single 0 parameter.

ESC [ <n> mSGRSet Graphics RenditionSet the format of the screen and text as specified by <n>

The following table of values can be used in <n> to represent different formatting modes.

Formatting modes are applied from left to right. Applying competing formatting options will result in the right-most option taking precedence.

For options that specify colors, the colors will be used as defined in the console color table which can be modified using the SetConsoleScreenBufferInfoEx API. If the table is modified to make the “blue” position in the table display an RGB shade of red, then all calls to Foreground Blue will display that red color until otherwise changed.

0DefaultReturns all attributes to the default state prior to modification
1Bold/BrightApplies brightness/intensity flag to foreground color
4UnderlineAdds underline
24No underlineRemoves underline
7NegativeSwaps foreground and background colors
27Positive (No negative)Returns foreground/background to normal
30Foreground BlackApplies non-bold/bright black to foreground
31Foreground RedApplies non-bold/bright red to foreground
32Foreground GreenApplies non-bold/bright green to foreground
33Foreground YellowApplies non-bold/bright yellow to foreground
34Foreground BlueApplies non-bold/bright blue to foreground
35Foreground MagentaApplies non-bold/bright magenta to foreground
36Foreground CyanApplies non-bold/bright cyan to foreground
37Foreground WhiteApplies non-bold/bright white to foreground
38Foreground ExtendedApplies extended color value to the foreground (see details below)
39Foreground DefaultApplies only the foreground portion of the defaults (see 0)
40Background BlackApplies non-bold/bright black to background
41Background RedApplies non-bold/bright red to background
42Background GreenApplies non-bold/bright green to background
43Background YellowApplies non-bold/bright yellow to background
44Background BlueApplies non-bold/bright blue to background
45Background MagentaApplies non-bold/bright magenta to background
46Background CyanApplies non-bold/bright cyan to background
47Background WhiteApplies non-bold/bright white to background
48Background ExtendedApplies extended color value to the background (see details below)
49Background DefaultApplies only the background portion of the defaults (see 0)
90Bright Foreground BlackApplies bold/bright black to foreground
91Bright Foreground RedApplies bold/bright red to foreground
92Bright Foreground GreenApplies bold/bright green to foreground
93Bright Foreground YellowApplies bold/bright yellow to foreground
94Bright Foreground BlueApplies bold/bright blue to foreground
95Bright Foreground MagentaApplies bold/bright magenta to foreground
96Bright Foreground CyanApplies bold/bright cyan to foreground
97Bright Foreground WhiteApplies bold/bright white to foreground
100Bright Background BlackApplies bold/bright black to background
101Bright Background RedApplies bold/bright red to background
102Bright Background GreenApplies bold/bright green to background
103Bright Background YellowApplies bold/bright yellow to background
104Bright Background BlueApplies bold/bright blue to background
105Bright Background MagentaApplies bold/bright magenta to background
106Bright Background CyanApplies bold/bright cyan to background
107Bright Background WhiteApplies bold/bright white to background

Extended Colors

Some virtual terminal emulators support a palette of colors greater than the 16 colors provided by the Windows Console. For these extended colors, the Windows Console will choose the nearest appropriate color from the existing 16 color table for display. Unlike typical SGR values above, the extended values will consume additional parameters after the initial indicator according to the table below.

SGR SubsequenceDescription
38 ; 2 ; <r> ; <g> ; <b>Set foreground color to RGB value specified in <r>, <g>, <b> parameters*
48 ; 2 ; <r> ; <g> ; <b>Set background color to RGB value specified in <r>, <g>, <b> parameters*
38 ; 5 ; <s>Set foreground color to <s> index in 88 or 256 color table*
48 ; 5 ; <s>Set background color to <s> index in 88 or 256 color table*

*The 88 and 256 color palettes maintained internally for comparison are based from the xterm terminal emulator. The comparison/rounding tables cannot be modified at this time.

Screen Colors

The following command allows the application to set the screen colors palette values to any RGB value.

The RGB values should be hexadecimal values between 0 and ff, and separated by the forward-slash character (e.g. rgb:1/24/86).

Note that this sequence is an OSC “Operating system command” sequence, and not a CSI like many of the other sequences listed, and as such start with “x1b]”, not “x1b[”.

ESC ] 4 ; <i> ; rgb : <r> / <g> / <b> ESCModify Screen ColorsSets the screen color palette index <i> to the RGB values specified in <r>, <g>, <b>

Mode Changes

These are sequences that control the input modes. There are two different sets of input modes, the Cursor Keys Mode and the Keypad Keys Mode. The Cursor Keys Mode controls the sequences that are emitted by the arrow keys as well as Home and End, while the Keypad Keys Mode controls the sequences emitted by the keys on the numpad primarily, as well as the function keys.

Each of these modes are simple boolean settings – the Cursor Keys Mode is either Normal (default) or Application, and the Keypad Keys Mode is either Numeric (default) or Application.

See the Cursor Keys and Numpad & Function Keys sections for the sequences emitted in these modes.

ESC =DECKPAMEnable Keypad Application ModeKeypad keys will emit their Application Mode sequences.
ESC >DECKPNMEnable Keypad Numeric ModeKeypad keys will emit their Numeric Mode sequences.
ESC [ ? 1 hDECCKMEnable Cursor Keys Application ModeKeypad keys will emit their Application Mode sequences.
ESC [ ? 1 lDECCKMDisable Cursor Keys Application Mode (use Normal Mode)Keypad keys will emit their Numeric Mode sequences.

Query State

All commands in this section are generally equivalent to calling Get* console APIs to retrieve status information about the current console buffer state.

Note These queries will emit their responses into the console input stream immediately after being recognized on the output stream while ENABLE_VIRTUAL_TERMINAL_PROCESSING is set. The ENABLE_VIRTUAL_TERMINAL_INPUT flag does not apply to query commands as it is assumed that an application making the query will always want to receive the reply.

ESC [ 6 nDECXCPRReport Cursor PositionEmit the cursor position as: ESC [ <r> ; <c> R Where <r> = cursor row and <c> = cursor column
ESC [ 0 cDADevice AttributesReport the terminal identity. Will emit “x1b[?1;0c”, indicating 'VT101 with No Options'.


While the windows console traditionally expects tabs to be exclusively eight characters wide, *nix applications utilizing certain sequences can manipulate where the tab stops are within the console windows to optimize cursor movement by the application.

The following sequences allow an application to set the tab stop locations within the console window, remove them, and navigate between them.

ESC HHTSHorizontal Tab SetSets a tab stop in the current column the cursor is in.
ESC [ <n> ICHTCursor Horizontal (Forward) TabAdvance the cursor to the next column (in the same row) with a tab stop. If there are no more tab stops, move to the last column in the row. If the cursor is in the last column, move to the first column of the next row.
ESC [ <n> ZCBTCursor Backwards TabMove the cursor to the previous column (in the same row) with a tab stop. If there are no more tab stops, moves the cursor to the first column. If the cursor is in the first column, doesn’t move the cursor.
ESC [ 0 gTBCTab Clear (current column)Clears the tab stop in the current column, if there is one. Otherwise does nothing.
ESC [ 3 gTBCTab Clear (all columns)Clears all currently set tab stops.
  • For both CHT and CBT, <n> is an optional parameter that (default=1) indicating how many times to advance the cursor in the specified direction.
  • If there are no tab stops set via HTS, CHT and CBT will treat the first and last columns of the window as the only two tab stops.
  • Using HTS to set a tab stop will also cause the console to navigate to the next tab stop on the output of a TAB (0x09, ‘t’) character, in the same manner as CHT.

Designate Character Set

The following sequences allow a program to change the active character set mapping. This allows a program to emit 7-bit ASCII characters, but have them displayed as other glyphs on the terminal screen itself. Currently, the only two supported character sets are ASCII (default) and the DEC Special Graphics Character Set. See http://vt100.net/docs/vt220-rm/table2-4.html for a listing of all of the characters represented by the DEC Special Graphics Character Set.

ESC ( 0Designate Character Set – DEC Line DrawingEnables DEC Line Drawing Mode
ESC ( BDesignate Character Set – US ASCIIEnables ASCII Mode (Default)

Notably, the DEC Line Drawing mode is used for drawing borders in console applications. The following table shows what ASCII character maps to which line drawing character.

HexASCIIDEC Line Drawing

Scrolling Margins

The following sequences allow a program to configure the “scrolling region” of the screen that is affected by scrolling operations. This is a subset of the rows that are adjusted when the screen would otherwise scroll, for example, on a ‘n’ or RI. These margins also affect the rows modified by Insert Line (IL) and Delete Line (DL), Scroll Up (SU) and Scroll Down (SD).

The scrolling margins can be especially useful for having a portion of the screen that doesn’t scroll when the rest of the screen is filled, such as having a title bar at the top or a status bar at the bottom of your application.

For DECSTBM, there are two optional parameters, <t> and <b>, which are used to specify the rows that represent the top and bottom lines of the scroll region, inclusive. If the parameters are omitted, <t> defaults to 1 and <b> defaults to the current viewport height.

Scrolling margins are per-buffer, so importantly, the Alternate Buffer and Main Buffer maintain separate scrolling margins settings (so a full screen application in the alternate buffer will not poison the main buffer’s margins). Computer applications for apple mac os x 10.7.5browser for mac os x 10 7 5.

ESC [ <t> ; <b> rDECSTBMSet Scrolling RegionSets the VT scrolling margins of the viewport.

Window Title

The following commands allows the application to set the title of the console window to the given <string> parameter. The string must be less than 255 characters to be accepted. This is equivalent to calling SetConsoleTitle with the given string.

Note that these sequences are OSC “Operating system command” sequences, and not a CSI like many of the other sequences listed, and as such starts with “x1b]”, not “x1b[”.

ESC ] 0 ; <string> BELSet Icon and Window TitleSets the console window’s title to <string>.
ESC ] 2 ; <string> BELSet Window TitleSets the console window’s title to <string>.

The terminating character here is the “Bell” character, ‘x07’

Alternate Screen Buffer

*Nix style applications often utilize an alternate screen buffer, so that they can modify the entire contents of the buffer, without affecting the application that started them. The alternate buffer is exactly the dimensions of the window, without any scrollback region.

For an example of this behavior, consider when vim is launched from bash. Vim uses the entirety of the screen to edit the file, then returning to bash leaves the original buffer unchanged.

ESC [ ? 1 0 4 9 hUse Alternate Screen BufferSwitches to a new alternate screen buffer.
ESC [ ? 1 0 4 9 lUse Main Screen BufferSwitches to the main buffer.

Window Width

The following sequences can be used to control the width of the console window. They are roughly equivalent to the calling the SetConsoleScreenBufferInfoEx console API to set the window width.

C++ Printf String

ESC [ ? 3 hDECCOLMSet Number of Columns to 132Sets the console width to 132 columns wide.
ESC [ ? 3 lDECCOLMSet Number of Columns to 80Sets the console width to 80 columns wide.

Soft Reset

The following sequence can be used to reset certain properties to their default values.The following properties are reset to the following default values (also listed are the sequences that control those properties):

  • Cursor visibility: visible (DECTEM)
  • Numeric Keypad: Numeric Mode (DECNKM)
  • Cursor Keys Mode: Normal Mode (DECCKM)
  • Top and Bottom Margins: Top=1, Bottom=Console height (DECSTBM)
  • Character Set: US ASCII
  • Graphics Rendition: Default/Off (SGR)
  • Save cursor state: Home position (0,0) (DECSC)
ESC [ ! pDECSTRSoft ResetReset certain terminal settings to their defaults.

Input Sequences

The following terminal sequences are emitted by the console host on the input stream if the ENABLE_VIRTUAL_TERMINAL_INPUT flag is set on the input buffer handle using the SetConsoleMode flag.

There are two internal modes that control which sequences are emitted for the given input keys, the Cursor Keys Mode and the Keypad Keys Mode. These are described in the Mode Changes section.

Cursor Keys

KeyNormal ModeApplication Mode
Up ArrowESC [ AESC O A
Down ArrowESC [ BESC O B
Right ArrowESC [ CESC O C
Left ArrowESC [ DESC O D

Additionally, if Ctrl is pressed with any of these keys, the following sequences are emitted instead, regardless of the Cursor Keys Mode:

KeyAny Mode
Ctrl + Up ArrowESC [ 1 ; 5 A
Ctrl + Down ArrowESC [ 1 ; 5 B
Ctrl + Right ArrowESC [ 1 ; 5 C
Ctrl + Left ArrowESC [ 1 ; 5 D

Numpad & Function Keys

Backspace0x7f (DEL)
Pause0x1a (SUB)
Escape0x1b (ESC)
InsertESC [ 2 ~
DeleteESC [ 3 ~
Page UpESC [ 5 ~
Page DownESC [ 6 ~
F5ESC [ 1 5 ~
F6ESC [ 1 7 ~
F7ESC [ 1 8 ~
F8ESC [ 1 9 ~
F9ESC [ 2 0 ~
F10ESC [ 2 1 ~
F11ESC [ 2 3 ~
F12ESC [ 2 4 ~


Alt is treated by prefixing the sequence with an escape: ESC <c> where <c> is the character passed by the operating system. Alt+Ctrl is handled the same way except that the operating system will have pre-shifted the <c> key to the appropriate control character which will be relayed to the application.

Ctrl is generally passed through exactly as received from the system. This is typically a single character shifted down into the control character reserved space (0x0-0x1f). For example, [email protected] (0x40) becomes NUL (0x00), Ctrl+[ (0x5b) becomes ESC (0x1b), etc. A few Ctrl key combinations are treated specially according to the following table:

Ctrl + Space0x00 (NUL)
Ctrl + Up ArrowESC [ 1 ; 5 A
Ctrl + Down ArrowESC [ 1 ; 5 B
Ctrl + Right ArrowESC [ 1 ; 5 C
Ctrl + Left ArrowESC [ 1 ; 5 D

Note Left Ctrl + Right Alt is treated as AltGr. When both are seen together, they will be stripped and the Unicode value of the character presented by the system will be passed into the target. The system will pre-translate AltGr values according to the current system input settings.


Example of SGR terminal sequences

The following code provides several examples of text formatting.

Note In the previous example, the string 'x1b[31m' is the implementation of ESC [ <n> m with <n> being 31.

The following graphic shows the output of the previous code example.

Example of Enabling Virtual Terminal Processing

The following code provides an example of the recommended way to enable virtual terminal processing for an application. The intent of the sample is to demonstrate:

  1. The existing mode should always be retrieved via GetConsoleMode and analyzed before being set with SetConsoleMode.

  2. Checking whether SetConsoleMode returns 0 and GetLastError returns STATUS_INVALID_PARAMETER is the current mechanism to determine when running on a down-level system. An application receiving STATUS_INVALID_PARAMETER with one of the newer console mode flags in the bit field should gracefully degrade behavior and try again.

Example of Select Anniversary Update Features

The following example is intended to be a more robust example of code using a variety of escape sequences to manipulate the buffer, with an emphasis on the features added in the Anniversary Update for Windows 10.

This example makes use of the alternate screen buffer, manipulating tab stops, setting scrolling margins, and changing the character set.