Escape-последовательности
Escape-последовательности используются для представления определённых специальных символов в строковых литералах и символьных константах.
Доступны следующие escape-последовательности. ISO C требует диагностику, если после обратной косой черты стоит символ, не указанный ниже:
| Escape- последовательность |
Описание | Представление |
|---|---|---|
| Простые escape-последовательности | ||
\'
|
апостроф | байт 0x27 в кодировке ASCII
|
\"
|
двойная кавычка | байт 0x22 в кодировке ASCII
|
\?
|
знак вопроса | байт 0x3f в кодировке ASCII
|
\\
|
backslash | byte 0x5c in ASCII encoding
|
\a
|
audible bell | byte 0x07 in ASCII encoding
|
\b
|
backspace | byte 0x08 in ASCII encoding
|
\f
|
form feed - new page | byte 0x0c in ASCII encoding
|
\n
|
line feed - new line | byte 0x0a in ASCII encoding
|
\r
|
carriage return | byte 0x0d in ASCII encoding
|
\t
|
horizontal tab | byte 0x09 in ASCII encoding
|
\v
|
vertical tab | byte 0x0b in ASCII encoding
|
| Numeric escape sequences | ||
\nnn
|
arbitrary octal value | byte nnn
|
\xnn
|
arbitrary hexadecimal value | byte nn
|
| Universal character names | ||
\unnnn (начиная с C99)
|
Unicode value in allowed range; may result in several code units |
code point U+nnnn
|
\Unnnnnnnn (начиная с C99)
|
Unicode value in allowed range; may result in several code units |
code point U+nnnnnnnn
|
Range of universal character namesIf a universal character name corresponds to a code point that is not 0x24 ( |
(начиная с C99) |
Notes
\0 is the most commonly used octal escape sequence, because it represents the terminating null character in null-terminated strings.
The new-line character \n has special meaning when used in text mode I/O: it is converted to the OS-specific newline byte or byte sequence.
Octal escape sequences have a length limit of three octal digits but terminate at the first character that is not a valid octal digit if encountered sooner.
Hexadecimal escape sequences have no length limit and terminate at the first character that is not a valid hexadecimal digit. If the value represented by a single hexadecimal escape sequence does not fit the range of values represented by the character type used in this string literal or character constant (char, char16_t, char32_t (начиная с C11), or wchar_t), the result is unspecified.
|
A universal character name in a narrow string literal or a 16-bit string literal (начиная с C11) may map to more than one code unit, e.g. |
(начиная с C99) |
|
A universal character name corresponding to a code pointer greater than 0x10FFFF (which is undefined in ISO/ISC 10646) can be used in character constants and string literals. Such usage is not allowed in C++20. |
(начиная с C99) (до C23) |
The question mark escape sequence \? is used to prevent trigraphs from being interpreted inside string literals: a string such as "??/" is compiled as "\", but if the second question mark is escaped, as in "?\?/", it becomes "??/"
Example
#include <stdio.h>
int main(void)
{
printf("This\nis\na\ntest\n\nShe said, \"How are you?\"\n");
}
Вывод:
This
is
a
test
She said, "How are you?"
References
- Стандарт C17 (ISO/IEC 9899:2018):
- 5.2.2 Character display semantics (стр. 18-19)
- 6.4.3 Universal Character names (стр. 44)
- 6.4.4.4 Character constants (стр. 48-50)
- Стандарт C11 (ISO/IEC 9899:2011):
- 5.2.2 Character display semantics (стр. 24-25)
- 6.4.3 Universal Character names (стр. 61)
- 6.4.4.4 Character constants (стр. 67-70)
- Стандарт C99 (ISO/IEC 9899:1999):
- 5.2.2 Character display semantics (стр. 19-20)
- 6.4.3 Universal Character names (стр. 53)
- 6.4.4.4 Character constants (стр. 59-61)
- Стандарт C89/C90 (ISO/IEC 9899:1990):
- 2.2.2 Character display semantics
- 3.1.3.4 Character constants
See also
Документация C++ по Escape sequences
|