In this blog, we will study the purpose of escape sequence in C++ programming language with the help of examples to implement them and their types and uses.
Escape Sequence
Escape sequences in C++ are character combinations that comprise a backslash (\) followed by some character or symbol. They give results such as getting to the following line or a TAB space.
The table of Escape sequences of C++ is as follows:
Escape sequence | Name |
---|---|
\a | Alarm (Beep, bell) |
\b | Backspace |
\f | Form Feed |
\r | Carriage Return |
\n | New line |
\t | Horizontal tab |
\v | Vertical tab |
\’ | Single quote mark |
\” | Double quote mark |
\? | Question mark |
Here is a details explanation of each
\a
\a escape sequence is used to play the beep during the program’s execution.
example
cout << "Geek\aPeak";
OUTPUT
GeekPeak
In this example, the compiler will play the beep after displaying Geek and then print the Peak on the screen.
\b
\b escape sequence is used to delete the previous character.
Example
cout << "Geek\bPeak";
OUTPUT
GeePeak
In this example, the compiler will display the output Geek on-screen, delete the last character then print the remaining output on screen: GeePeak.
\f
\f escape sequence is used to skip to the start of the next page.
It applies mainly to terminals where the output device is a printer rather than a VDU.
\n
\n is used to jump to the next line.
example
cout << "Jum to the \nnew line" <<endl;
output
Jum to the
new line
For more about the new line character, refer to this blog and also a detailed explanation about endl and many more.
\r
\r called carriage return display the output after erasing the number of elements from the start, same as after the carriage return escape sequence and reverse it. Let’s understand it with an example.
example
cout << "This is for the carriage return \rescape sequence" <<endl;
output
escape sequence carriage return
In the above example, the compiler first displays the output after the carriage return escape sequence, then the remaining after deduction of the number of elements from the start, same as after the carriage return sequence.
There were 15 characters after the \a, including spaces, and 32 before the \a, so the compiler deducts the 15 characters, including spaces from 32, and prints the remaining elements.
another example
cout << "Geek on peak is on the \rtop of lallantop" <<endl;
output
top of lallantopon the
\t
\t is used to insert the tab(horizontal tab) in string or text.
example
cout << "Geekon\tPeak" <<endl;
output
Geekon Peak
\v
\v is used to insert the vertical tab in string or text.
example
cout << "Escape sequence" <<endl;
cout << "\vGeekonPeak" <<endl;
output
Escape sequence
GeekonPeak
\’
\’ is used to display the single quote mark in the text.
example
cout << "Geek\'s Property" <<endl;
output
Geek's Peak
\”
\” is used to display the double quotation mark in the text.
example
cout << "\"GeekonPeak\"" <<endl;
output
"GeekonPeak"
\?
\? is used to display the question mark in the text.
example
cout << "Who is he\?" <<endl;
output
Who is he?
Follow up
So here we got to know all about the escape sequence in the C++ programming language. The escape sequence made the programming easier to express some symbols and many more without any problem. So, you’ve done it well. Practice it once on your compiler/onlinegdb to understand and grasp it well, as coding is all about practicality, and theory alone doesn’t render the result and enhance the learning.
Similarly, you must also know that GeekonPeak has just begun and is looking for some loyal supporters so we, the new-gen geeky friends, can grow and create more extensive and easy-to-grasp courses for you (for free). You can follow us on Facebook, Instagram & Twitter as well. Also, don’t miss our newsletter subscription. We provide exclusive blogs, tips, tricks, and advice to program efficiently and market smartly. We also promote some of our partners with excellent experiences to share with you. Until next time 🙂.