In C and C++ source code, you need to escape certain characters if you want to use them in string literals. Various other languages and tools have adopted some or all of these as well:
Sequence | Description | ASCII code (decimal) |
---|---|---|
\0 | Null | 0 |
\\ | Backslash | 7 |
\” | Double quotation mark | 34 |
\’ | Single quotation mark | 39 |
\? | Question mark | 63 |
\a | Alarm / Bell | 7 |
\b | Backspace | 8 |
\f | Form feed | 12 |
\n | Line feed (new line) | 10 |
\r | Carriage return | 13 |
\t | Tab (horizontal) | 9 |
\v | Tab (vertical) | 11 |
In addition to the standard escape sequences above, it is commonly also possible to use ASCII codes directly. Note that they need to be expressed in octal or hexadecimal though:
Base | Sequence | Examples | |
---|---|---|---|
Octal (8) | \nnn | Line feed: Question mark: |
\012 \077 |
Hexadecimal (16) | \xnn | Line feed: Question mark: |
\x0a \x3f |
Code example:
printf("The \"quick\" brown fox\077jumps over\nthe lazy dog\3f");
That should output:
The "quick" brown fox jumps over the lazy dog?