Escape sequences

From Cppreference

Jump to: navigation, search

Escape sequences are used to define certain special characters within string literals.

The following escape sequences are available:

Escape
sequence
Description
\' single quote (byte 0x27)
\" double quote (byte 0x22)
\\ backslash (byte 0x5c)
\0 null character (byte 0x00)
\a audible bell (byte 0x07)
\b backspace (byte 0x08)
\f form feed - new page (byte 0x0c)
\n line feed - new line (byte 0x0a)
\r carriage return (byte 0x0d)
\t horizontal tab (byte 0x09)
\v vertical tab (byte 0x0b)
\nnn arbitrary octal value (byte nnn)
\xnn arbitrary hexadecimal value (byte nn)
\unnnn arbitrary Unicode value (code point U+nnnn).
May result in several characters.
\Unnnnnnnn arbitrary Unicode value (code point U+nnnnnnnn)
May result in several characters.

[edit] Notes

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 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 (char, char16_t, char32_t, or wchar_t), the result is unspecified.

A universal character name in a narrow string literal may map to more than one char due to multibyte encoding.

[edit] Example

#include <iostream>
 
int main()
{
    std::printf("This\nis\na\ntest\n\nShe said, \"How are you?\"\n");}

Output:

This
is
a
test
 
She said, "How are you?"

[edit] See also

ASCII chart