Escape sequences in C
Escape sequences are used in the programming languages C and C++, and their design was copied in many other languages such as Java and C#. An escape sequence is a sequence of characters that does not represent itself when used inside a character or string literal, but is translated into another character or a sequence of characters that may be difficult or impossible to represent directly.
In C, all escape sequences consist of two or more characters, the first of which is the backslash, ; the remaining characters determine the interpretation of the escape sequence. For example, is an escape sequence that denotes a newline character.
Motivation
Suppose we want to print out on one line, followed by on the next line. One could attempt to represent the string to be printed as a single literal as follows:- include
This is not valid in C, since a string literal may not span multiple logical source lines. This can be worked around by printing the newline character using its numerical value,
- include
This instructs the program to print, followed by the byte whose numerical value is, followed by. While this will indeed work when the machine uses the ASCII encoding, it will not work on systems that use other encodings, that have a different numerical value for the newline character. It is also not a good solution because it still does not allow to represent a newline character inside a literal, and instead takes advantage of the semantics of printf. In order to solve these problems and ensure maximum portability between systems, C interprets inside a literal as a newline character, whatever that may be on the target system:
- include
In this code, the escape sequence does not stand for a backslash followed by the letter, because the backslash causes an "escape" from the normal way characters are interpreted by the compiler. After seeing the backslash, the compiler expects another character to complete the escape sequence, and then translates the escape sequence into the bytes it is intended to represent. Thus, represents a string with an embedded newline, regardless of whether it is used inside or anywhere else.
This raises the issue of how to represent an actual backslash inside a literal. This is done by using the escape sequence, as seen in the next section.
Some languages don't have escape sequences, for example Pascal. Instead a command including a newline would be used.
writeln;
write;
Table of escape sequences
The following escape sequences are defined in standard C. This table also shows the values they map to in ASCII. However, these escape sequences can be used on any system with a C compiler, and may map to different values if the system does not use a character encoding based on ASCII.Escape sequence | Hex value in ASCII | Character represented |
07 | Alert | |
08 | Backspace | |
1B | Escape character | |
0C | Formfeed Page Break | |
0A | Newline ; see notes 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 | |
any | The byte whose numerical value is given by nnn interpreted as an octal number | |
any | The byte whose numerical value is given by hh… interpreted as a hexadecimal number | |
none | Unicode code point below 10000 hexadecimal | |
none | Unicode code point where h is a hexadecimal digit |
Non-standard escape sequences
A sequence such as is not a valid escape sequence according to the C standard as it is not found in the table above. The C standard requires such "invalid" escape sequences to be diagnosed. Notwithstanding this fact, some compilers may define additional escape sequences, with implementation-defined semantics. An example is the escape sequence, which has 1B as the hexadecimal value in ASCII, represents the escape character, and is supported in GCC, clang and tcc. It wasn't however added to the C standard repertoire, because it has no meaningful equivalent in some character sets.Universal character names
From the C99 standard, C has also supported escape sequences that denote Unicode code points in string literals. Such escape sequences are called universal character names, and have the form or, where stands for a hex digit. Unlike the other escape sequences considered, 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. The code point is converted into a sequence of code units in the encoding of the destination type on the target system. For example, consider
char s1 = "\xC0";
char s2 = "\u00C1";
wchar_t s3 = L"\xC0";
wchar_t s4 = L"\u00C0";
The string will contain a single byte whose numerical value, the actual value stored in memory, is in fact. The string will contain the character "Á", U+00C1. On a system that uses the UTF-8 encoding, the string will contain two bytes,. The string contains a single, again with numerical value. The string contains the character "À" encoded into, if the UTF-16 encoding is used, then will also contain only a single, 16 bits long, with numerical value. A universal character name such as 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. Again, always denotes the character at code point 1F60316, regardless of context. On the other hand, 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 "physical" code units, universal character names represent code points, which may be thought of as "logical" characters.