Escape sequences – special characters

Escape sequences in C++ are special characters that are used to represent non-printable or reserved characters. They are also used to format output by inserting special characters in the output stream. The backslash () is used to indicate the beginning of an escape sequence.

Escape sequences are useful in situations where you need to insert non-printable or special characters in your output. For example, if you want to output a string that contains double quotes, you can use the escape sequence ” to represent the double quote character without closing the string.

It’s important to note that not all characters can be escaped in C++. For example, you cannot use the escape sequence \c to represent the character ‘c’. If you try to use an invalid escape sequence, you will get a compiler error.

In conclusion, escape sequences in C++ are a useful tool for formatting output and representing non-printable or reserved characters. Understanding how to use them is an important part of learning C++ programming.

CharacterMeaning
\’Single quote
\”Double quote
\?Question mark
\\Backslash
\bBackspace
\fNew page
\nNew line
\rCarriage return
\tHorizontal tab
\vVertical tab
Example
#include <iostream>
using namespace std;

int main() {
    cout << "Heading:\n\'single quote\'\n\"double quote\"\nDo you have questions\?" << endl;
}
Output
Heading:
'single quote'
"double quote"
Do you have questions?