1. Intro
An escape sequence is a special combination of characters, always starting with a backslash (\), used inside a character or string literal to represent a character that can't easily be typed directly — like a newline, a tab, or a literal backslash itself.
Cricket analogy: An escape sequence is like a scorer using a special symbol (W) to represent 'wicket' on the scoresheet since you can't literally draw a falling stump — the backslash in \n similarly stands in for a newline that can't be typed directly into a string.
2. Syntax
'\X' // inside a char literal
"text\Xtext" // inside a string literal3. Explanation
The most commonly used escape sequences in C++ are:
Cricket analogy: Just as a scorecard has a fixed set of common shorthand symbols — W for wicket, NB for no-ball, WD for wide — C++ has its own fixed set of commonly used escape sequences.
- \n — newline (moves cursor to the next line)
- \t — horizontal tab
- \\ — a literal backslash character
- \' — a literal single quote
- \" — a literal double quote
- \0 — the null character (marks the end of a C-style string)
- \r — carriage return
- \a — alert / bell sound
Escape sequences are necessary because certain characters are either invisible (like a newline or tab) or already have special meaning in code — a double quote, for instance, is what marks the start and end of a string literal, so "She said "hi"" would confuse the compiler. Escaping it as "She said \"hi\"" tells the compiler to treat that quote as literal text, not as the end of the string.
Cricket analogy: Escape sequences matter because certain marks already have special meaning, like how a scorer can't just write a literal quote inside a quoted commentary line 'He said "what a shot"' without the scorebook's quote-tracking getting confused about where the quote ends — escaping it as \" fixes that.
Pro Tip
because the backslash itself starts an escape sequence, to print an actual backslash character you must escape it too: \\.
4. Example
#include <iostream>
using namespace std;
int main() {
cout << "Line one\nLine two" << endl;
cout << "Name:\tAlex" << endl;
cout << "She said \"Hello!\"" << endl;
cout << "Path: C:\\Users\\Alex" << endl;
return 0;
}5. Output
Line one
Line two
Name: Alex
She said "Hello!"
Path: C:\Users\Alex6. Key Takeaways
- An escape sequence starts with a backslash and represents a special or hard-to-type character.
- \n (newline) and \t (tab) are the most frequently used escape sequences.
- Escaping quotes (\" or \') lets you include that quote character literally inside a string.
- To print a literal backslash, you must escape it as \\.
Practice what you learned
1. What character does every C++ escape sequence begin with?
2. Which escape sequence inserts a newline?
3. How would you print a literal double quote inside a string?
4. What does \0 represent?
5. How do you print a literal backslash character?
Was this page helpful?
You May Also Like
Literals in C++
Learn what literals are in C++ — integer, floating-point, character, string, and boolean literals — with syntax and examples.
Data Types in C++
Learn the fundamental data types in C++ — int, float, double, char, bool, and void — with sizes, ranges, and examples.
Comments in C++
Learn what comments are in C++, the difference between single-line and multi-line comments, and best practices for using them.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics