Some characters in C++ have unique meanings. These characters are escape sequences that perform some special function, and they begin with a \
(Backslash is called escape character) followed by a letter
or number
. The output doesn’t display these characters.
Different Types of Escape Sequence in C++
In C++, there are several escape sequences. The following are the most regularly used escape sequences in C++:
\n
: escape sequence character is used in C++ to insert a new line in the output.
For example,
cout << “Learn With\nTechABU” ;
//Output
Learn With
TechABU
\t
: escape sequence character is used in C++ to insert a TAB in output.
For example,
cout << “Learn With\tTechABU” ;
//Output
Learn With TechABU
\a
: is an escape sequence used in C++ to generate a signal to alert (alarm).
cout<< “Welcome To The \a Future” ;
In the above line of code, the compiler will first print “Welcome To The” and then alert a signal, and then it will print “Future.”
List of All Escape Sequences Used In C++
Name | Symbol | Purpose/Meaning |
---|---|---|
Alert | \a | Makes an alarm sound, such as a beep. |
Backspace | \b | Backspaces the cursor one space. |
Formfeed | \f | Moves the cursor to next logical page. |
Newline | \n | Moves the cursor to the next line. |
Carriage Return | \r | Moves cursor to the beginning of the line. |
Horizontal Tab | \t | Inserts a Horizontal Tab |
Vertical Tab | \v | Inserts a Vertical Tab |
Single Quote | \' | Prints a Single Quote. |
Double Quote | \" | Prints a Double Quote. |
Backslash | \\ | Prints a backslash |
Question Mark | \? | Prints a question mark. |
Octal/Hex Number | \number | Convert to a char that is represented by an octal/hex number. |
Example: A Simple Program Demonstrating Escape Sequences
#include <iostream> using namespace std; int main() { cout << "I'm learning...\tC++ Programming" << endl; return 0; }
OUTPUT:
I'm learning... C++ Programming