Escape sequences are 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. |
|---|
\ooo | Octal number | Represents an octal number. |
|---|
\xhh | Hexadecimal number | Represents a hexadecimal number. |
|---|
Note: The output of some of these escape sequences depends upon the compiler you are working on.
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++
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 7873923408
Example 2: Program to demonstrate how to use \b escape sequence in C++
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
Hi Geeks
Example 3: Program to demonstrate how to use \n escape sequence in C++
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;
}
OutputHello
GeeksforGeeks
Example 4: Program to demonstrate how to use \t escape sequence in C++
C++
#include <iostream>
using namespace std;
int main()
{
// Inserts a horizontal tab space
cout << "Hello \t GFG";
return 0;
}
Example 5: Program to demonstrate how to use \v escape sequence in C++
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++
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
GeeksforGeeks
Example 7: Program to demonstrate how to use \\ escape sequence in C++
C++
#include <iostream>
using namespace std;
int main()
{
// Inserts a backslash character
cout << "Hello\\GFG";
return 0;
}
Example 8: Program to demonstrate how to use \' and \" escape sequences in C++
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++
C++
#include <iostream>
using namespace std;
int main()
{
// Displays a question mark
cout << "\?\?!\n";
return 0;
}
Example 10: Program to demonstrate how to use \ooo escape sequence in C++
C++
#include <iostream>
using namespace std;
int main()
{
// Represents an octal number
char* s = "A\072\065";
cout << s;
return 0;
}
Example 11: Program to demonstrate how to use \xhh escape sequence in C++
C++
#include <iostream>
using namespace std;
int main()
{
// Represents a hexadecimal number
char* s = "B\x4a";
cout << s;
return 0;
}
Explore
C++ Basics
Core Concepts
OOP in C++
Standard Template Library(STL)
Practice & Problems