C++ for beginners: An Introduction to escape Sequences

January 23, 2017
All, C++
by Rahil Shaikh

Escape sequences are special characters used in control string to modify the format of an output. These specific characters are translated into another character or a sequence of characters that may be difficult to represent directly. For example, you want to put a line break in the output of a C++ statement then you will use “\n” character which is an escape sequence itself.

This a guest article on ciphertrick.com

An escape sequence consists of two or more characters. For all sequences, the first character will be ”\” i.e. backslash. The other characters determine the interpretation of escape sequence. For example, “n” of “\n” tells the cursor to move on the next line.

New Line (\n):

When a new line is necessary in the output, then this escape sequence is used. For example:


First of all, “COMPUTER” is printed and”\n” shifts the cursor to the next line. Then “SCIENCE” is printed on second line. A screenshot of output is shown below:

C++ New Line (\n)

C++ New Line (\n)

Tab (\t):

A TAB is equal to eight spaces. Whenever TAB button is pressed from the keyboard, then 8 spaces are left blank. This escape sequence performs the functionality of TAB key in the output stream. The code given below will insert a TAB between two words.


C++ Tab (\t)

C++ Tab (\t)

As you can see after printing “COMPUTER”, 8 spaces are left blank and then “SCIENCE” is printed on the screen.

Alert Bell (\a):

This escape sequence is used to play beep during execution. For example:


First of all, “COMPUTER” is printed then a beep is played and after that “SCIENCE” is printed.

Backspace (\b):

Whenever we want to delete a single character, we press the button “backspace” from our keyboard. The same functionality can be achieved in C++ output with this escape sequence. For example:


C++ Backspace (\b)

C++ Backspace (\b)

First of all, “COMPUTER” is printed and after that “\b” comes which deletes the last character i.e. “R”. After that, “SCIENCE” is printed.

Single Quote (\’):

To insert a single quote in the output, this escape sequence is used. Look at the code written below:

This code prints single quotes at the start and end of the string.

C++ Double Quote (\”)

C++ Double Quote (\”)

Carriage Return (\r):

This escape sequence moves the cursor at the beginning of current line. For example:


First of all, “COMPUTER” is printed and after that “\r” comes which moves the cursor at the beginning of the line and “SCIENCE” is printed which overwrites the word written before.

Carriage Return (\r)

Carriage Return (\r)

Form Feed (\f)

This escape sequence is used in printing. It is used to insert a blank paper in the printed output. For example:


After printing “COMPUTER”, the computer will include a blank paper and then print “SCIENCE”.


This article helped us understand some of the important escape sequences in C++. This is a kind of information a C++ beginner is expected to understand and know.

This is a guest post by Kamal Choudhary:

Kamal Choudhary is a tech geek who writes C++ programming tutorials on his blog. He is a student of computer science in the University of Gujrat, Pakistan. He loves to write about computer programming. You can find his full bio here. Follow him on twitter @ikamalchoudhary


Engineer. Blogger. Thinker. Loves programming and working with emerging tech. We can also talk on Football, Gaming, World Politics, Monetary Systems.

Get notified on our new articles

Subscribe to get the latest on Node.js, Angular, Blockchain and more. We never spam!
First Name:


  1. |

    thanks bro it helped me in my first assignment please add more stuff and make and android app for this website

    • Kirubakaran

      Where is a ? , \ , ” , v, ooo, XHH?

  2. |

    Plz let me know that how can I program or add an Interface in my C++ project.

  3. |

    Can you explain the escape sequence to represent hexadecimal number and octal number with examples

    • |

      Hexadecimal \xHw
      Octal no \On

  4. |

    thnk u sir…… ALLAH bless u AMEEN

  5. Avinash Kumar

    Thanks for sharing this step by step information about C++ and read your complete post, you have explained in really very understanding method so keep posting such informative blog post.

    • |

      Thank you for the explanation here. It really helped me.

      Could you please explain why we use the escape character \?

  6. |

    The Output of the carriage Return is wrong. I think it should be Sciencer

Leave a Comment