What is the Escape Sequences ?

What is the Escape Sequences ?

Escape sequences are special characters used to control the format of the output. These characters are used in output statement. They are not displayed in the output.

An escape sequence begins with backslash" that is called escape character The backslash causes an escape from the normal interpreting of the characters in C++ That is why they are called escape sequences.

Different escape sequences used in C++ are as follows:

Escape Sequence

Purpose

\a

Alarm

\b

Backspace

\f

Form feed

\n

New line

\r

Carriage return

\t

Tab

\’

Single quote

\”

Double quote


Alarm ( \a )

This escape sequence is used to play beep during the execution. An example of \a escape sequence is as follows:

  • cout<<"Hello \a World";

The above line will display "Hello", play a beep and then print "World".

Backspace ( \b )

This escape sequence is used to insert backspace in the output. An example of \b escape sequence is as follows: 

  • cout<<"Hello\bWorld";

The above line will display "Hello". The \b will delete the letter 'o' Then the word "World" will appear. The output will appear as "HellWorld".

Form feed ( \f )

This escape sequence is used to insert a blank paper in the printed output. It is called form feed It is used during printing. An example of escape sequence is as follows:

  • cout<<"Hello \f World";

The above line will print "Hello" and the computer will include a blank paper. Then the word "World" will appear.

New line ( \n )

This escape sequence is used to insert new line in output. An example of \n escape sequence is as follows: 

  • cout<<"Hello \n World";

The above line will display "Hello". The escape sequence \n will shift the cursor to next line. Then the word "World" will appear The output is displayed on two lines.

Carriage return ( \r ) 

This escape sequence is used to move the cursor at the beginning of current line. An example of \r escape sequence is as follows: 

  • cout<<"Hello\r  World";

The above line will display "Hello". The escape sequence \r will move the cursor at the beginning of current line. Then the word "World" will appear that overwrites "Hello" So only "World" is displayed on screen.

Tab ( \t )

This escape sequence is used to insert a TAB in the output. An example of \t escape sequence is as follows. 

  • cout<<"Hello \t World");

The above line will display "Hello" The escape sequence \t will insert a TAB. Then the word "World" will appear The output will appear as "Hello World".

Single quote ( \' )

This escape sequence is used to display single quotes in the output. An example of escape sequence is as follows: 

  • cout<<" '\ Hello World\ ' ";

The above line will display "Hello World" on the screen.

Double quote ( \" )

This escape sequence is used to display double quotes in the output. An example of \" escape sequence is as follows.

  • cout<<"\"Hello World!"/ ";

The above line will display "Hello World" on the screen.

Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.