Escape sequences are a special way to write characters that can't be typed directly. It starts with a backslash (\) followed by a letter or symbol. They are used inside strings or characters to control formatting or show special symbols.
Escape Sequences in C++
The following table lists the commonly used escape characters in C++:
| Escape Sequence | Name | Description |
|---|---|---|
\\ | Backslash | Inserts a backslash character. |
\' | Single quote | Displays a single quotation mark. |
\" | Double quote | Displays double quotation marks. |
\? | Question mark | Displays a question mark. |
\a | Alert (bell) | Generates a bell sound in the C++ program. |
\b | Backspace | Moves the cursor one place backward. |
\f | Form feed | Moves the cursor to the start of the next logical page. |
\n | New line | Moves the cursor to the start of the next line. |
\r | Carriage return | Moves the cursor to the start of the current line. |
\t | Horizontal tab | Inserts some whitespace to the left of the cursor and moves the cursor accordingly. |
\v | Vertical tab | Inserts vertical space. |
\0 | Null character | Represents the NULL character. Note: It can consume up to two following digits (0-7) as part of an octal sequence. |
\ooo | Octal number | Represents an octal number. |
\xhh | Hexadecimal number | Represents a hex value. Note: Consumes all following hex digits greedily. |
Note: The output of some of these escape sequences depends upon the compiler you are working on.
The Octal 3-Digit Rule
In C++, an octal escape sequence (\) will consume a maximum of three digits ($0-7$). This means \0 is not a special standalone command, it is the start of an octal sequence.
- If you write "\012", the compiler does not see a Null character followed by "12". It sees Octal 012, which is a Newline character.
- To fix it, separate a null character from following numbers, use string concatenation: "\0" "12".
Examples of Escape Characters in C++
The following examples demonstrates the use of above escape sequences in C++:
Example 1: Program to demonstrate how to use \a escape sequence in C++
#include <iostream>
using namespace std;
int main()
{
// Generates a bell sound
cout << "My mobile number is "
"7\a8\a7\a3\a9\a2\a3\a4\a0\a8\a";
return 0;
}
Output
My mobile number is 7873923408Example 2: Program to demonstrate how to use \b escape sequence in C++
#include <iostream>
using namespace std;
int main()
{
// Moves the cursor one place backward
cout << "Hello \b\b\b\b\b\bHi Geeks";
return 0;
}
Output
HHi GeeksBehavior of \b with/without endl:
Note: \b only moves the cursor back without endl some terminals partially repaint the line (so leftover characters may be hidden), while with endl the output is flushed and fully repainted, making the remaining characters visible.
Example 3: Program to demonstrate how to use \n escape sequence in C++
#include <iostream>
using namespace std;
int main()
{
// Moves the cursor to the start of the next line
cout << "Hello\n";
cout << "GeeksforGeeks";
return 0;
}
Output
Hello GeeksforGeeks
Example 4: Program to demonstrate how to use \t escape sequence in C++
#include <iostream>
using namespace std;
int main()
{
// Inserts a horizontal tab space
cout << "Hello \t GFG";
return 0;
}
Output
Hello GFG
Example 5: Program to demonstrate how to use \v escape sequence in C++
#include <iostream>
using namespace std;
int main()
{
// Inserts a vertical tab space
cout << "Hello friends\v";
cout << "Welcome to GFG";
return 0;
}
Output
Hello friends
Welcome to GFG
Example 6: Program to demonstrate how to use \r escape sequence in C++
#include <iostream>
using namespace std;
int main()
{
// Moves the cursor to the start of the current line
cout << "Hello Geeks \rGeeksfor";
return 0;
}
Output
GeeksforGeeksExample 7: Program to demonstrate how to use \\ escape sequence in C++
#include <iostream>
using namespace std;
int main()
{
// Inserts a backslash character
cout << "Hello\\GFG";
return 0;
}
Output
Hello\GFG
Example 8: Program to demonstrate how to use \' and \" escape sequences in C++
#include <iostream>
using namespace std;
int main()
{
// Displays a single quotation mark
cout << "\' Hello Geeks\n";
// Displays double quotation marks
cout << "\" Hello Geeks";
return 0;
}
Output
' Hello Geeks " Hello Geeks
Example 9: Program to demonstrate how to use \? escape sequence in C++
#include <iostream>
using namespace std;
int main()
{
// Displays a question mark
cout << "\?\?!\n";
return 0;
}
Output
??!
Example 10: Program to demonstrate how to use \ooo escape sequence in C++
#include <iostream>
using namespace std;
int main()
{
// Represents an octal number
char* s = "A\072\065";
cout << s;
return 0;
}
Output
A:5
Example 11: Program to demonstrate how to use \xhh escape sequence in C++
#include <iostream>
using namespace std;
int main() {
const char* s = "\x42" "J"; // Hex 42 is 'B'
cout << "Result: " << s << endl;
return 0;
}
Output
Result: BJ