In today's tutorials, we are going to have a look at escape sequence in C++. In our previous tutorial, Introduction to C++ we have designed a small Hello World program and if you haven't studied it yet then you should first check that one out as I am gonna take it further from there.

In that tutorial, we have designed a code which prints the Hello world on the output screen. The code used for printing it on the output screen is as follows:

How to use Escape Sequence in C++ ??

  • Now, suppose if I want to print out "Hello World" , instead of Hello World.
  • In simple words, I wanna add "" these symbols as well on each side of Hello World.
  • But if you add them in the above program then it will generate errors.
  • So, what to do now ? Here comes the benefit of escape sequence in c++.
  • The escape sequence used to print "" these symbols in output is this symbols which is also know as back slash.
  • So, now by using this escape sequence, the code will look like something as shown below:
#include <iostream>
 
using namespace std;
 
int main(void)
{
     cout<<""Hello World!!!""<<endl;
     return 0;
}
  • Now in this way we can quite easily print the "Hello World" on the screen.
  • Now suppose you want to print itself then again what you need to do is to write two back slashes like this .
  • The first back slash is the escape sequence while the second one will print as it is.
  • There are many other uses of this back slash like if we add n after this escape sequence then it will work as a new line so instead of using this endl, we can use n as shown in below code:
#include <iostream>
 
using namespace std;
 
int main(void)
{
     cout<<""Hello World!!!"n";
     return 0;
}
  • Now you can see in the above code I have used n and removed the endl and still it will work the same.
  • Now if you want to and a TAB in the output then you can use t.
  • So, there are many benefits of this escape sequence in c++.
  • Similarly r is used for carriage return.
  • v is used for vertical tab.
  • b is used for backspace.
These are different escape sequence uses in C++ and I hope you have got it now that How to use Escape sequence in c++. In the coming tutorial, I will explain How and why to use comments in C++. That's all for today, take care and have fun !!! :)


JLCPCB – Prototype 10 PCBs for $2 (For Any Color)

China’s Largest PCB Prototype Enterprise, 600,000+ Customers & 10,000+ Online Orders Daily
How to Get PCB Cash Coupon from JLCPCB: https://bit.ly/2GMCH9w

Syed Zain Nasir

I am Syed Zain Nasir, the founder of <a href=https://www.TheEngineeringProjects.com/>The Engineering Projects</a> (TEP). I am a programmer since 2009 before that I just search things, make small projects and now I am sharing my knowledge through this platform.I also work as a freelancer and did many projects related to programming and electrical circuitry. <a href=https://plus.google.com/+SyedZainNasir/>My Google Profile+</a>

Share
Published by
Syed Zain Nasir