An escape sequence contains a backslash (\) symbol followed by one of the escape sequence characters or an octal or hexadecimal number. A hexadecimal escape sequence contains an x followed by one or more hexadecimal digits (0 -9, A-F, a-f). An octal escape sequence uses up to three octal digits (0-7) .
Note: The line continuation sequence (\ followed by a new-line character) is not an escape sequence. It is used in character strings to indicate that the current line continues on the next line.
The escape sequences and the characters they represent are:
/-----------------------------------\ | ESCAPE | CHARACTER REPRES- | | SEQUENCE | ENTED | |------------+----------------------| | "\a" | Alert (bell, alarm) | |------------+----------------------| | "\b" | Backspace | |------------+----------------------| | "\f" | Form feed (new page) | |------------+----------------------| | "\n" | New-line | |------------+----------------------| | "\r" | Carriage return | |------------+----------------------| | "\t" | Horizontal tab | |------------+----------------------| | "\v" | Vertical tab | |------------+----------------------| | "\'" | Single quotation | | | mark | |------------+----------------------| | "\"" | Double quotation | | | mark | |------------+----------------------| | "\?" | Question mark | |------------+----------------------| | "\\" | Backslash | \-----------------------------------/
The value of an escape sequence represents the member of the character set used at run time. For example, on a system uses the ASCII character codes, the letter V is represented by the escape sequence \x56.
Use escape sequences only in character constants or in string literals.
If an escape sequence is not recognized, the compiler uses the character following the backslash and a message is issued. Note that this behavior is implementation-defined.
In string and character sequences, when you want the backslash to represent
itself (rather than the beginning of an escape sequence), you must use a \\
backslash escape sequence. For example:
cout << "The escape sequence \\n." << endl;
This statement results in the following output:
The escape sequence \n.
The following program prints the character 'a' four times to standard
output, and then prints a new line:
#include <iostream.h>
void main()
{
char a,b,c,d,e;
a='a';
b=97; // ASCII integer value
c='\141'; // ASCII octal value
d='\x61'; // ASCII hexadecimal value
e='\n';
cout << a << b << c << d << e;
}
Related Information
Character Constants
String Literals