Escape Sequence in C++ with Examples

escape sequence

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”;



Escape Sequence Characters:

\n:

‘n’ stands for a new line. It inserts a new line and the cursor moves to 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<<”Programming\tDigest”;

The output of the statement will be as under:

Programming     digest

‘\a’:

‘a’ stands for alarm. It causes  a beep sound in 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 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 \”.

cout<<”\”welcome\””;

The output will be as under:

“welcome”


Programming Examples:

Example write a program that displays the following output:

‘D’          ‘I’            ‘g’           ‘e’           ‘s’           ‘t’

“Programming”

escape sequence

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

escape sequence


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

escape sequence

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

escape sequence



Example write a program that displays square, a cube of a number in table form:

escape sequence

Related Article:

C++ Programming Example of Basic Input and Output stream objects

Recommended For You

Fawad

About the Author: Fawad

My name is Shahzada Fawad and I am a Programmer. Currently, I am running my own YouTube channel "Expertstech", and managing this Website. My Hobbies are * Watching Movies * Music * Photography * Travelling * gaming and so on...

Leave a Reply

%d bloggers like this: