Escape Sequence in C++ with Examples

(Last Updated On: April 4, 2021)

The Escape Sequence:

The escape sequences are special non-printing characters that are used to control the printing behavior of the output stream objects (such as ‘cout’). These characters are not displayed in the output. An escape sequence is prefixed with a backslash () and a coded character is used to control the printing behavior. The backslash () is called an escape character. So the escape sequence looks like two characters.

The escape sequence is used inside a string constant or independently. These are written in single or double-quotes. The escape sequence can be inserted in any position of the string such as:

  • At the beginning of the string.
  • In the middle of the string.
  • At the end of the string etc.

For example, the escape sequence ‘\n’ is used to insert a new line. The cursor moves from the current position on the output device to the beginning of the next line. If escape sequence ‘\n’ is inserted at the beginning of the string then the string will be printed after printing a blank line e.g.

Cout<<”\nWelcome”;


Amazon Purchase Links:

Top Gaming Computers

Best Laptops

Best Graphic Cards

Portable Hard Drives

Best Keyboards

Best High Quality PC Mic

Computer Accessories

*Please Note: These are affiliate links. I may make a commission if you buy the components through these links. I would appreciate your support in this way!

Escape Sequence Characters:

\n:

‘n’ stands for a new line. It inserts a new line and the cursor moves from the beginning to the next line.

\t:

‘t’ stands for tab. It moves the cursor one horizontal tab from the current tab stop to the next tab stop. For example, an output statement is given below using escape sequence ‘\t’

Cout<<”Electronic\tClinic”;

The output of the statement will be as under:

Electronic     Clinic

‘\a’:

‘a’ stands for alarm. It causes a beep sound on the computer.

‘\b’:

‘b’ stands for backspace. It moves the cursor one space back. For example, a statement is given below using ‘\b’ escape sequence.

Cout<<”welcome\b”;

After execution of this statement, the string “welcome” will be printed and the cursor will move one space back. It means that it will be under the character ‘e’ of the word “welcome”. In the above statement if ‘\b’ is omitted, then the cursor will be at the end of the string. If another output statement is used after the above statement, then the printing will be started from character ‘e’ and ‘e’ will be deleted.

‘\r’:

‘r’ stands for return. It moves the cursor to the beginning of the current line.

‘\f’:

‘f’ stands for from feed. It caused the output to feed one paper on the printer attached to the computer.

\:

It is used to insert a backslash character in the output. For example, a statement is given below using ‘\’ escape sequence.

Cout<<”\Programming\”;

The output will be as under:

\Programming\

If s single backslash character is used, then there will be no effect on the output.

‘\’ :

It is used to insert a single quotation mark in the output. For example, a statement is given below using ‘\’ escape sequence.

Cout<<”\’welcom\’”;

The output will be as under:

‘welcome’

“ \” “:

It is used to insert a double quotation mark in the output. For example, a statement is given below using \” escape sequence.

Cout<<”\”welcome\””;



The output will be as under:

“welcome”

For example, write a program that displays the following output using an escape sequence:

C’          ‘L’            ‘i’           ‘n’           ‘i’           ‘c’

“Electronic”

escape sequence

Example write a program that displays the triangle pattern using a single output statement(without using a loop) using Escape Sequence:

escape sequence


Example write a program that displays the arrowhead pattern using a single output statement (without using a loop) using Escape Sequence:

escape sequence

Example write a program that displays the 5×2 matrix using a single output statement (without using a loop) using Escape Sequences:

escape sequence


Example write a program that displays square, the cube of a number in table form using Escape Sequences:

escape sequence

Recommended For You

About the Author: Engr Fahad

My name is Shahzada Fahad and I am an Electrical Engineer. I have been doing Job in UAE as a site engineer in an Electrical Construction Company. Currently, I am running my own YouTube channel "Electronic Clinic", and managing this Website. My Hobbies are * Watching Movies * Music * Martial Arts * Photography * Travelling * Make Sketches and so on...

Leave a Reply

%d bloggers like this: