Escape Sequence in C++ with Examples
The Escape Sequence:
The escape sequences are special non-printing characters that are used to control the printing behavior of the output stream objects (such as ‘cout’). These characters are not displayed in the output. An escape sequence is prefixed with a backslash (\) and a coded character is used to control the printing behavior. The backslash (\) is called an escape character. So the escape sequence looks like two characters.
The escape sequence is used inside a string constant or independently. These are written in single or double-quotes. The escape sequence can be inserted in any position of the string such as:
- At the beginning of the string.
- In the middle of the string.
- At the end of the string etc.
For example, the escape sequence ‘\n’ is used to insert a new line. The cursor moves from the current position on the output device to the beginning of the next line. If escape sequence ‘\n’ is inserted at the beginning of the string then the string will be printed after printing a blank line e.g.
cout<<”\nWelcome”;
Escape Sequence Characters:
\n:
‘n’ stands for a new line. It inserts a new line and the cursor moves to the beginning to the next line.
\t:
‘t’ stands for tab. It moves the cursor one horizontal tab from the current tab stop to the next tab stop. For example, an output statement is given below using escape sequence ‘\t’
cout<<”Programming\tDigest”;
The output of the statement will be as under:
Programming digest
‘\a’:
‘a’ stands for alarm. It causes a beep sound in the computer.
‘\b’:
‘b’ stands for backspace. It moves the cursor one space back. For example, a statement is given below using ‘\b’ escape sequence.
cout<<”welcome\b”;
After execution of this statement, the string “welcome” will be printed and the cursor will move one space back. It means that it will be under the character ‘e’ of the word “welcome”. In the above statement if ‘\b’ is omitted, then the cursor will be at the end of string. If another output statement is used after the above statement, then the printing will be started from character ‘e’ and ‘e’ will be deleted.
‘\r’:
‘r’ stands for return. It moves the cursor to the beginning of the current line.
‘\f’:
‘f’ stands for from feed. It caused the output to feed one paper on the printer attached to the computer.
\\:
It is used to insert a backslash character in the output. For example, a statement is given below using ‘\\’ escape sequence.
cout<<”\\Programming\\”;
The output will be as under:
\Programming\
If s single backslash character is used, then there will be no effect on the output.
‘\’ :
It is used to insert a single quotation mark in the output. For example, a statement is given below using ‘\’ escape sequence.
cout<<”\’welcom\’”;
The output will be as under:
‘welcome’
” \” “:
It is used to insert a double quotation mark in the output. For example, a statement is given below using \”.
cout<<”\”welcome\””;
The output will be as under:
“welcome”
Programming Examples:
Example write a program that displays the following output:
‘D’ ‘I’ ‘g’ ‘e’ ‘s’ ‘t’
“Programming”
1 2 3 4 5 6 7 8 9 |
#include <iostream> using namespace std; int main() { cout<<"\'D\'\t\'i\'\t\'g\'\t\'e\'\t\'s\'\t\'t\'\n"; cout<<"\"Programming\"\n"; } |
Example write a program that displays the triangle pattern using a single output statement(without using a loop):
1 2 3 4 5 6 7 8 |
#include <iostream> using namespace std; int main() { cout<<"*\n* *\n* * *\n* * * *\n"; } |
Example write a program that displays the arrowhead pattern using a single output statement (without using a loop) :
1 2 3 4 5 6 7 8 |
#include <iostream> using namespace std; int main() { cout<<" *\n *\t*\n *\t*\t*\n *\t*\n *"; } |
Example write a program that displays the 5×2 matrix using a single output statement (without using a loop):
1 2 3 4 5 6 7 |
#include <iostream> using namespace std; int main() { cout<<" 1\t 3\t 5\t 7\t 9\n 11\t 13\t 15\t 12\t 19 "; } |
Example write a program that displays square, a cube of a number in table form:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
#include <iostream> using namespace std; int main() { cout<<" Number\t Square\t Cube "<<endl; cout<<" 1\t"<<1*1<<"\t"<<1*1*1<<endl; cout<<" 2\t"<<2*2<<"\t"<<2*2*2<<endl; cout<<" 3\t"<<3*3<<"\t"<<3*3*3<<endl; cout<<" 4\t"<<4*4<<"\t"<<4*4*4<<endl; } |
Related Article:
https://programmingdigest.com/c-programming-example-of-basic-input-and-output-stream-objects/
It awesome 🙌
This is great