How to use Escape Sequence in C++

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 !!! :)

Introduction to C++

Hello friends, hope you all are fine and having fun with your lives. Today, I am going to start a new series of tutorials on C++ Programming. and here's my first tutorial in this series which is Introduction to C++. I am gonna share a lot of tutorials in this series in future, in which I am gonna explain all about C++. In the initials tutorials, we will cover the basics of C++ Programming and later on we will also cover the pro concepts of C++ Programming.

I am planning on posting around 20 to 30 tutorials in this C++ Programming series, and I am quite sure that it will cover all about C++ and if you are a new learner then it will help you quite a lot.. I have started this series on a request of one of my readers. He suggested me this idea and I like it quite a lot so I though to pursue it. So, today let's have a look at Introduction to C++, which is quite essential when you are learning a new language you must have its introduction first.

Introduction to C++

  • So, now let me first write a simple C++ code, which is gonna print Hello World on the screen.
  • Below is given the very basic C++ code and I am gonna explain this code below to give an Introduction to C++ in detail:
#include <iostream>

using namespace std;

int main(void)
{
     cout<<"Hello World!!!"<<endl;
     return 0;
}
  • The above code is the simplest C++ code which is gonna print Hello World on the screen.
  • If you read the above code from start then you can see the first statement in the above code is #include <iostream>.
  • This first statement is actually including a library in our code file.
  • The C++ Compiler already has a lot of libraries in it which we use in our program and can get benefit out of it.
  • So, now question is what are these libraries. In any compiler the libraries are designed to create functions and then you can use these functions quite easily.
  • Let me explain it in a simple way. For example you want to add two numbers 2 + 2, now you know that the operator + is used for addition but the C++ won't know about it unless you add the library for math.
  • So, here we want to print something on our screen so the C++ will not print it on the screen unless we include this iostream library in it.
  • There are many builtin libraries for C++ like conio, arithmetic etc. which we will cover in later tutorials.
  • But for rite now, I think you have got the idea what is library, and you really don't need to know what they are doing. :)
  • Next line used is using namespace std, its a namespace standard library and you just have to add it as it is in the C++ Library.
  • Now next we have the int main(void) command. Its basically a function, which is called the main function.
  • In C++ the compiler works top to bottom and the first thing it goes into is the main function so your code must have the main function otherwise it will generate error.
  • This Main function is of the form as shown below:
int main(void)
{
  
}
  • Now in this main function, you can add anything you wanna add or execute.
  • We can create many functions in C++ coding which we will surely cover in coming tutorials but the Main function will always remain single means you can't add another Main function in it.
  • Now after the Main function, we have added a line whose initials are cout, its a c++ commands which is used to print something out and you can see rite after cout we have written a string "Hello World!!!".
  • So, because of cout command our code is printing this Hello World on the screen.
  • If you notice we have << these signs between cout and our string to print.
  • These signs are called insertion operators.
  • At the end of this statement we have endl, which is called end line, and its similar to pressing enter button and after it we have our semi colon (;). Semi colon tells the C++ that our statement is ended.
  • So, as a whole its called a statement, and in this statement we first printed out Hello World!!! and then we Entered and finally we added a semi colon to tell our program that statement has ended.
  • Now in the next statement we have return 0; , it will return nothing back and will just stop.
  • So, in our simple above program we have Main function with two statements and its printing Hello World on the screen.
  • It was kind of an Introduction to C++ in which we have designed a small program and then discussed it.
So, that's all about the introduction to C++, and I hope you guys have learned something out of it. In the coming tutorials, I am gonna post more about C++ and we will cover about variable used in it and how we can make complex codes on c++. So stay tuned and have fun !!! :)
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