Escape Sequence in C++

What is Escape Sequence in C++:

They are used especially to perform some special operations like going to a new line, providing a horizontal tab, vertical tab, etc.


These characters followed by a backslash().
The following is a list of escape sequences.

Sr.no Escape SequenceMeaning
1.\nNewline
2.\tHorizontal tab
3.\vVertical tab
4.\bBackspace
5.\rCarriage return
6.\fForm feed
7.\aAudible alert(bell)
8.\\Backslash
9.\?Question mark
10\’Single quote
11\”Double quote
12\eescape character
13\xhhHexadecimal number
14\nnnOctal number
15\0Null character

Here Are The Explanation For The Following Character :

  1. \n ( Newline): We use this type of character for moving the cursor to the new line
  2. \t (Horizontal tab): We use this type of character for moving the cursor leaving a couple of space
  3. \v (Vertical tab)
  4. \b (Backspace)
  5. \r (Carriage return): We use this type of character to locate the cursor to the start of the current line.
  6. \f (Form feed page break)
  7. \a (Audible alert(bell)): We use this type of character to produce beep type of audible bell so that the user can get the indication about the execution of the program
  8. \\ (Backslash): We use this type of character to display the backslash character
  9. \? (Question mark) : We use this type of character to display the question mark?
  10. \’ (Single quote): We use this type of character for displaying a single quote in the text
  11. \” (Double quote): We use this type of character for displaying the double quote in the text
  12. \e (the escape character)
  13. \xhh (Hexadecimal number): We use this type of character for displaying Hexadecimal number
  14. \nnn (Octal number): We use this type of character for displaying Octal number
  15. \o (Null character): We use this type of character for termination of the string

Welcome to C++! It can be printed in several ways. uses multiple stream insertion statements, yet produces identical output to the program. This works because each stream-insertion statement resumes printing where the previous statement stopped printing. The first stream insertion print Welcome followed by a space and the second stream insertion begins printing on the same line immediately. In general, C++ allows the programmer to express statements in a variety of ways

A single statement can print multiple lines by using newline characters as inline. Each time the \n(newline) escape sequence is encountered in the output stream, the screen cursor is positioned to the beginning of the next line. To get a blank line in your output, place two newline character back to back

1. Write a Program using the new Line character (\n)

//Printing a line with multiple statement
#include<iostream>
//function main begins program execution
int main()
{
std::cout<<"Welcome\nto\n\nC++!\n";
return 0;     //indicate that program ended successfully
}  //end function main

OUTPUT:

Welcome  to  C++! 

2. Write a Program using Horizontal tab (\t)

#include<iostream>  
int main() 
{ 
std::cout<<"Welcome\tto\tC++"; 
return 0;    
}  

OUTPUT:

 Welcome  to  C++! 

3. Write a Program using Vertical tab(\v)

#include<iostream>
 int main()
 {
 std::cout<<"Welcome\vto\vC++!\v";
 return 0;     
 }  

OUTPUT:

Welcome
       to
         C++!

4. Write a Program using Backspace(\b)

#include<iostream>
int main()
{
std::cout<<"Welcome\bto\bC++!\b";
return 0;     
}  

OUTPUT:

WelcomtC++! 

5. Write a Program using Carriage return(\r)

#include<iostream>
int main()
{
std::cout<<"Welcome\rto\rC++!\r";
return 0;     
}

OUTPUT:

 C++!ome

6. Write a Program using Form feed page break(\f)

#include<iostream>
int main()
{
std::cout<<"Welcome\fto\fC++!\f";
return 0;     
} 

OUTPUT:

Welcome
       to
         C++!

7. Write a Program using Audible Alert(bell)(\a)

#include<iostream>
int main()
{
std::cout<<"Welcome\ato\aC++!\a";
return 0;     
}  

OUTPUT:

WelcometoC++!

8. Write a Program using Backslash(\\)

#include<iostream>
int main()
{
std::cout<<"Welcome\\to\\C++!\\";
return 0;     
}   

OUTPUT:

Wlecome\to\C++!\

9. Write a Program using a Question mark(\?)

 #include<iostream>
int main()
{
std::cout<<"Welcome\?to\?C++!\?";
return 0;     
}    

OUTPUT:

Welcome?to?C++!?

10. Write a Program using a Single Quote(\’)

#include<iostream>
int main()
{
std::cout<<"Welcome\'to\'C++!\'";
return 0;     
}    

OUTPUT:

Wlcome'to'C++!'

11. Write a program using Double Quotes(\”)

 #include<iostream>
int main()
{
std::cout<<"Welcome\"to\"C++!\"";
return 0;     
}     

OUTPUT:

Welcome"to"C++!"

12. Write a Program using Escape Character(\e)

#include<iostream>
int main()
{
std::cout<<"Welcome\eto\eC++!\e";
return 0;     
}     

OUTPUT:

 Welcomeo++!

13. Write a Program using Hexadecimal number(\xhh)

#include<iostream>
int main()
{
std::cout<<"J\x4a";
return 0;     
}      

OUTPUT:

JJ

14. Write a Program using Octal Number(\nnn)

#include<iostream>
int main()
{
std::cout<<"\121\101";
return 0;     
}      

OUTPUT:

QA

15. Write a Program using Null Character(\o)

#include<iostream>
int main()
 {
 std::cout<<"\000Automated Teller Machaine";
 return 0;     
 }  

OUTPUT:

No OUTPUT comes it used to terminating the string

Translate »