Himanshu

Negi

Developer & Internet Marketer


Welcome to my blog.

Sed sed leo sit amet elit sagittis fermentum. Nulla posuere commodo erat. Nam ac nunc ipsum, id espara tincidunt orci. Quisque adipiscing molestie dolor, ut congue sem porta et. Sed sapien urna, auctor et accumsan ut, accumsan sit amet dui. Nunc tristique interdum magna, dictum volutpat sem. Nam ac nunc ipsum, id espara tincidunt orci. Sed sapien urna, auctor et accumsan ut.

Escape Sequence in C++ Programming with Examples

April 29, 2017wp-himu0 Comments

Escape sequences in C++ Programming are character combinations which comprise a backslash (\) followed by some character. They give results such as getting to the next line or a TAB space. They are called escape sequences since the backslash causes an “escape” from the normal way characters are interpreted by the C++ compiler.

Let’s begin with a simple C++ program that display a message on screen with no escape sequence character used.

[codesyntax lang=”cpp” title=”C++ program to display message” bookmarkname=”C++ program to display message”]

#include<iostream.h> 
#include<conio.h>

Void main() 
{ 
       cout<<"Escape Sequence";
       getch();
}

[/codesyntax]

 

Question: How will you print your message – “Escape Sequence in C++ Programming” divided in two different lines?

[codesyntax lang=”cpp” title=”Escape Sequence in C++]

#include<iostream.h>
#include<conio.h>

Void main() 
{ 
     cout<<"Escape Sequence ";
     cout<<"in C++ Programming.";
     getch();
}

[/codesyntax]
Program Output:
Escape Sequence in C++ Programming.

The above program prints the complete string message in just one line instead of two. The reason is that you didn’t gave any instruction to print next cout string message in the next line.

Take a look at the following program to know how to print anything in a new line using C++ Escape Sequence Character.
[codesyntax lang=”cpp” title=”Use of Escape sequence in C++ program” ]

#include<iostream.h>
#include<conio.h>

Void main() 
{ 
       cout<<"First Name\nLast Name";
       getch();
}

[/codesyntax]

Notice that? “\n” between the double quotes. It’s an escape sequence in C++ used to display the output string message in a new line. Both the programs have identical output but the second program saves your time and effort, that’s what the programming is known for.

So are you able to guess the output of the following program? Note that I have used more than one C++ escape sequence character.
[codesyntax lang=”cpp” title=”Use of escape sequence character twice” ]

#include<iostream.h> 
#include<conio.h>

Void main() 
{ 
       cout<<"Himanshu\nNegi\nLoves C++"; 
}

[/codesyntax]

The output is shown below:
Himanshu
Negi
Loves C++

 

Significance of Escape sequence in C++ Programming?

What is the real importance and of escape sequences in a programming language? Now think about the situation when you wish to display a double quotation (“) mark on the screen? No, you can’t put it in cout statement as it will just terminate the cout message and nothing else happen other than getting an Error on Screen by compiler.

Example: cout<<“Quotaion Symbol inserted ” here”;           // Invalid

There is only way, use escape sequence character. The following C++ program puts some light on the situation.

[codesyntax lang=”cpp” title=”Important use of Escape Sequence in C++” ]

#include<iostream.h> 
#include<conio.h>

Void main() 
{ 
       cout<<"\" Displayed"; 
}

[/codesyntax]

NOTE: If you will simply put a quotation mark in between cout quotes then the compiler will get confused. Your quotation mark between quotes will be seen as an end of cout message by the compiler and the actual quotation mark (“) will be seen as an extra.

List of Some C++ Escape Sequence Characters:

This is the list of some known Escape Sequence in C++. Don’t forget to try them. Do some experiment and show your creativity.

\a – Bell(bep) \b – Backspace \f – Formfeed \n – New line \r – Carriage Return \t – Horizontal Tab \\ – Backslash \’ – Single Quotation Mark \” – Double Quatation Mark

Escape sequence in C++ Programming

Escape sequence in C++ Programming


Leave a Reply
www.edatastyle.com www.introducingsomething.com