100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
C++

Escape Sequences in C++

Learn what escape sequences are in C++, the full list of common ones, and how to use them inside strings and characters.

BasicsBeginner6 min readJul 7, 2026
Analogies

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

cpp
'\X'      // inside a char literal
"text\Xtext" // inside a string literal

3. 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

cpp
#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

text
Line one
Line two
Name:	Alex
She said "Hello!"
Path: C:\Users\Alex

6. 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

Was this page helpful?

Topics covered

#CStudyNotes#Programming#EscapeSequencesInC#Escape#Sequences#Syntax#Explanation#StudyNotes#SkillVeris#ExamPrep