Escape Sequences in C++
Escape Sequences in C++ are special character constants to represent non graphical characters in C++.
Most common non graphical characters include enter key, backspace, tab etc.
They start with a backward slash ( \ ) followed by a specific character.
Most commonly used escape sequences in C++ are:
1) \n
To insert one blank line where it appears.
#include<iostream> using namespace std; int main() { cout<<“Welcome”; cout<<“\nBye “; return 0; }
Output:
Welcome Bye
2) \t
To leave a gap of 8 spaces.
#include<iostream> using namespace std; int main() { cout<<“Welcome \t Bye “; return 0; }
Output
Welcome Bye
3) \b
To delete a character appearing before it.
#include<iostream> using namespace std; int main() { cout<<“Welcome\b Bye “; return 0; }
Output:
Welcom Bye
4) \\
Shows backward slash( \ ).
#include<iostream> using namespace std; int main() { cout<<“Welcome\\ Bye “; return 0; }
Output:
Welcome\ Bye
5 \’
Shows Single Quotes (‘) .
#include<iostream> using namespace std; int main() { cout<<“Father\'s name “; return 0; }
Output:
Father's name
6) \”
Shows Double Quotes (“ ) .
#include<iostream> using namespace std; int main() { cout<<"\"Welcome\""; return 0; }
Output:
"Welcome"
7) \0 (Back Slash followed by zero)
To terminate a string. Nothing after it will be shown.
#include<iostream> using namespace std; int main() { cout<<“Welcome\0 Bye “; return 0; }
Output:
Welcome