Website For Developers

Escape Sequence in C++



• Escape Sequences in C++ are used to display special characters in a string.
• These characters are not displayed in the output.
• Escape Sequences in C++ are used to modify the string.
• The list of certain special characters / escape sequence in C++ is as follow:

Escape sequences Description
\' This escape sequence in C++ is used for Single Quote.
\" This escape sequence in C++ is used for Double Quotes.
\? This escape sequence in C++ is used for Question Mark.
\n This escape sequence in C++ is used for New Line.
\t This escape sequence in C++ is used for Horizontal Tab.
\v This escape sequence in C++ is used for Vertical Tab.
\a This escape sequence in C++ is used for Alarm Beep.
\b This escape sequence in C++ is used for Backspace.
\f This escape sequence in C++ is used for New Page.
\r This escape sequence in C++ is used for Carriage Return.


Escape sequence in C++ example

#include<iostream>
#include<conio.h>
using namespace std;
int main() {
  cout<< "Hey, \"How are you\?\"" ;
  getch();
  return 0;
}

Output

Hey, "How are you?"


Write a program to show the following output
1  4  7
  2  5  8
    3  6  9

#include<iostream>
#include<conio.h>
using namespace std;
int main() {
  cout<< "1\t4\t7\n\t2\t5\t8\n\t\t3\t6\t9" ;
  getch();
  return 0;
}

Output

1  4  7
  2  5  8
    3  6  9


Write a program to display
      *
  *      *
*          *
  *      *
      *

#include<iostream>
#include<conio.h>
using namespace std;
int main() {
  cout<< "\t\t\t*\n\t\t*\t\t*\n\t*\t\t\t\t*\n" ;
  cout<< "\t\t*\t\t*\n\t\t\t*" ;
  getch();
  return 0;
}

Output

      *
  *      *
*          *
  *      *
      *