In the C programming language, an escape sequence is specially delimited text in a character or string literal that represents one or more other characters to the compiler. It allows a programmer to specify characters that are otherwise difficult or impossible to specify in a literal.

An escape sequence starts with a backslash () called the escape character and subsequent characters define the character. For example, denotes a newline character.

The same or similar escape sequences are used in other, related languages such as C++, C#, Java and PHP.

Compilation

An escape sequence changes how the compiler interprets character data in a literal. For example, does not represent a backslash followed by the letter . The backslash escapes the compiler's normal, literal way of interpreting character data. After a backslash, the compiler expects subsequent characters to complete one of the defined escape sequences, and then translates the escape sequence into the characters it represents.

This syntax does require special handling to encode a backslash character since it is a metacharacter that changes literal interpretation behavior; not the literal backslash character. The issue is solved by using two backslashes () to mean one.

Escape sequences

The following table includes escape sequences defined in standard C as well as some non-standard sequences. The C standard requires an escape sequence that does not match a defined sequence to be diagnosed i.e., the compiler must print an error message. Regardless, some compilers define additional escape sequences.

The table shows the ASCII value a sequence maps to, however, it may map to different values based on encoding.

Escape sequence Hex value in ASCII Character represented 07 Alert (Beep, Bell) (added in C89) 08 Backspace 1B Escape character 0C Formfeed Page Break 0A Newline (Line Feed); see below 0D Carriage Return 09 Horizontal Tab 0B Vertical Tab 5C Backslash 27 Apostrophe or single quotation mark 22 Double quotation mark 3F Question mark (used to avoid trigraphs) nnn (octal) The byte whose numerical value is given by nnn interpreted as an octal number hh… The byte whose numerical value is given by hh… interpreted as a hexadecimal number non-ASCII Unicode code point below 10000 hexadecimal (added in C99) non-ASCII Unicode code point where h is a hexadecimal digit

Escape

The non-standard sequence represents the escape character in GCC, clang and tcc. It was not added to the C standard because it has no meaningful equivalent in some character sets (such as EBCDIC).

Newline

Sequence maps to one byte, despite the fact that the platform may use more than one byte to denote a newline, such as the CRLF sequence, . The translation from to on MS-DOS and Windows occurs when the byte is written out to a file or to the console, and the inverse translation is done when text files are read.

Hex

A hex escape sequence must have at least one hex digit following , with no upper bound; it continues for as many hex digits as there are. Thus, for example, denotes the byte with the numerical value ABCDEF16, followed by the letter , which is not a hex digit. However, if the resulting integer value is too large to fit in a single byte, the actual numerical value assigned is implementation-defined. Most platforms have 8-bit types, which limits a useful hex escape sequence to two hex digits. However, hex escape sequences longer than two hex digits might be useful inside a wide character or wide string literal (prefixed with L): // single char with value 0x12 (18 decimal) char s1[] = "\x12"; // single char with implementation-defined value, unless char is long enough char s1[] = "\x1234"; // single wchar_t with value 0x1234, provided wchar_t is long enough (16 bits suffices) wchar_t s2[] = L"\x1234";

To separate a hex escape sequence from succeeding hex digits, use C string literal concatenation, e.g., .

Octal

An octal escape sequence consists of a backslash followed by one to three octal digits. The octal escape sequence ends when it either contains three octal digits, or the next character is not an octal digit. For example, is an octal escape sequence denoting a byte with decimal value 9 (11 in octal). However, is the octal escape sequence followed by the digit . In order to denote the byte with numerical value 1, followed by the digit , one must use .

Some three-digit octal escape sequences are too large to fit in a single byte. This results in an implementation-defined value for the resulting byte.

The escape sequence is a commonly used octal escape sequence, which denotes the null character, with value zero in ASCII and most encoding systems. It is still, however, an octal escape sequence; thus is a single SOH, not a NUL and a Digit One.

Universal character names

Since the C99 standard, C supports escape sequences that denote Unicode code points, called universal character names. They have the form or , where stands for a hex digit. Unlike other escape sequences, a universal character name may expand into more than one code unit.

The sequence denotes the code point , interpreted as a hexadecimal number. The sequence denotes the code point , interpreted as a hexadecimal number. Code points located at U+10000 or higher must be denoted with the syntax, whereas lower code points may use or . The code point is converted into a sequence of code units in the encoding of the destination type on the target system. For example, where the encoding is UTF-8, and UTF-16 for : // A single byte with the value 0xC0; not valid UTF-8 char s1[] = "\xC0"; // Two bytes with values 0xC3, 0x80; the UTF-8 encoding of U+00C0 char s2[] = "\u00C0"; // A single wchar_t with the value 0x00C0 wchar_t s3[] = L"\xC0"; // A single wchar_t with the value 0x00C0 wchar_t s4[] = L"\u00C0";

A value greater than may be represented by a single if the UTF-32 encoding is used, or two if UTF-16 is used.

Importantly, the universal character name always denotes the character "À", regardless of what kind of string literal it is used in, or the encoding in use. The octal and hex escape sequences always denote certain sequences of numerical values, regardless of encoding. Therefore, universal character names are complementary to octal and hex escape sequences; while octal and hex escape sequences represent code units, universal character names represent code points, which may be thought of as "logical" characters.

See also Escape sequence Digraph

References

Further reading ISO/IEC 9899:1999, Programming languages — C