Escape Sequence in C++
Escape sequences are used in output statement within format string. These are collection of characters that starts with backslash ( \ ). These characters are not displayed in output but has some special effect on output.
List of Escape Sequence
Some commonly used escape sequences are as.
No | Escape Sequence | Purpose/Impact |
1 | \n | It is used to insert in line in output. |
2 | \b | It is used as backspace in format string. |
3 | \t | It is used to add a tab ( Hrizontal ) in output. |
4 | \" | It is used to add double qoutes in output string. |
5 | \' | It is used to add single qoutes in output string. |
6 | \a | It is used as alert/beep. |
7 | \\ | It is used to add backslash in output string. |
8 | \r | It is used for carriage return in output. |
9 | \f | It is used as formatted page break in output. |
10 | \v | It is used to add a tab ( Vertical ) in output. |
Usage
Escape sequence are always used in format string. Format string is the text that is written in double quotes.
Example
#include <iostream>
using namespace std;
int main(int argc, char** argv) {
// Insert new line using \n
cout<<"Hello \n World";
// Insert tab using \t
cout<<"Hello \t World";
// Insert backslash in output
cout<<"Hello World \\";
return 0;
}