Escape Sequences

An escape sequence consists of a backslash symbol (\) followed by one of the escape sequence characters (or an octal or hexadecimal number). The commonly used escape sequences and their meaning are listed in the table below.

The C++ escape sequences
Escape sequenceMeaning
\aAlert (bell, alarm)
\fForm feed (new page)
\nNew line
\rCarriage return
\tHorizontal tab
\vVertical tab
\'Single quotation mark
\"Double quotation mark
\?Question mark
\0Null character

Escape sequences are used with string literals to represent special characters when writing C++ source code. For example, the following statement prints the statement "Here is some text." on two lines, and then inserts three blank lines.

cout << "Here is\nsome text.\n\n\n";

Note that the backslash is also used as a continuation marker to break long string literals into shorter sections to aid readability. The continuation marker is not an escape sequence, it simply tells the compiler that the string continues on the next line in the source code e.g.

#define MESSAGE "A long message that won't fit \
on one line in the C++ program editor."