

I use this normal sequence after outputting any color text, which halts further color output.
#C language ansi escape sequences code#
So blue text on a red background has this sequence: \0x1b[34 41m These codes can also be combined with other attributes, such as blinking and underline, if you really want to induce nausea in your users.Īnother good code to know is \x1b[m, which is the “normal” escape sequence. The color codes can be combined in a single sequence if separated by a semicolon. The nn is replaced by the color code values as listed in the following table. Here is the escape sequence as encoded in C: The escape sequence to generate colors starts with the escape character (get it?), followed by a left bracket, the color code value and a lowercase M. These can be foreground or background colors. If not, you can find alternative terminal apps available, some of which boast about their ANSI color capabilities.ĪNSI defines eight color codes representing basic text colors: black, red, green, yellow, blue, magenta, cyan, and white. The same type of sequences are used to generate color.īe aware that not every terminal program recognizes ANSI sequences. I wrote about ANSI in an blog post from 2020 regarding inverse text. The secret is ANSI, which uses escape sequences to encode various text attributes, among them colors. All you need to know are the secret codes that activate and deactivate the attributes.

The terminal flavor is what determines the color palette. Aside from adding wide characters, you can spice things up with color text. This is useful primarily for preprocessor definitions longer than a single line.Your C programs’ text output need not be so dull.

When a newline character (equivalent to pressing the RETURN key) immediately follows the backslash, the compiler ignores the backslash and the newline character and treats the next line as part of the previous line. You can also use the backslash ( \) as a continuation character. For instance, the vertical tab and form feed escape sequences ( \v and \f) do not affect screen output, but they do perform appropriate printer operations. Some escape sequences are device-specific. For example, the ESC character ( \033) is often used as the first character of a control command for a terminal or printer. For example, \c is treated as an c.Įscape sequences allow you to send nongraphic control characters to a display device. If a backslash precedes a character that does not appear in the table, the compiler handles the undefined character as the character itself. Unicode character in hexadecimal notation if this escape sequence is used in a wide-character constant or a Unicode string literal.įor example, WCHAR f = L'\x4e00' or WCHAR b = L"The Chinese character for one is \x4e00". Note that the question mark preceded by a backslash ( \?) specifies a literal question mark in cases where the character sequence would be misinterpreted as a trigraph. The following table lists the ANSI escape sequences and what they represent. They are also used to provide literal representations of nonprinting characters and characters that usually have special meanings, such as the double quotation mark ( "). An escape sequence is regarded as a single character and is therefore valid as a character constant.Įscape sequences are typically used to specify actions such as carriage returns and tab movements on terminals and printers. Character combinations consisting of a backslash ( \) followed by a letter or by a combination of digits are called "escape sequences." To represent a newline character, single quotation mark, or certain other characters in a character constant, you must use escape sequences.
