Cpp - Char Escape Sequences

Introduction

Nongraphic characters can be expressed by means of escape sequences, for example \t, which represents a tab.

An escape sequence always begins with a \ (backslash) and represents a single character.

You can use octal and hexadecimal escape sequences to create any character code.

Thus, the letter A (decimal 65) in ASCII code can also be expressed as \101 (three octals) or \x41 (two hexadecimals).

The characters ', ", and \ have no special meaning when preceded by a backslash, i.e. they can be represented as \', \", and \\respectively.

When using octal numbers for escape sequences in strings, be sure to use three digits, for example, \033 and not \33.

Single character

Meaning

ASCII code
(decimal)
\a
alert (BEL)
7
\b
backspace (BS)
8
\t
horizontal tab (HT)
9
\n
line feed (LF)
10
\v
vertical tab (VT)
11
\f
form feed (FF)
12
\r
carriage return (CR)
13
\"
" (double quote)
34
\'
' (single quote)
39
\?
? (question mark)
63
\\
\ (backslash)
92
\0
string terminating character
0
ooo(up to 3 octal digits)
numerical value of a character
ooo (octal!)
\xhh (hexadecimal digits)
numerical value of a character hh (hexadecimal!)

Demo

#include <iostream> 
using namespace std; 
int main() //from   w  ww  . ja v a 2  s . com
{ 
    cout << "\nThis is\t a string\n\t\t" 
         " with \"many\" escape sequences!\n"; 
    return 0; 
}

Result

Related Topic