C++ Escape Sequences Tutorial
Learn C++ escape sequences with practical examples. Understand newline, tab, quote, backslash, and other special characters used in output strings and beginner C++ programs.
What are Escape Sequences?
Escape sequences are special characters used inside a C++ string to perform a specific action or display special characters.
An escape sequence usually starts with a backslash (\).
Common Escape Sequences
| Escape Sequence | Name | Purpose |
|---|---|---|
\n |
Newline | Moves the cursor to the next line |
\t |
Tab | Adds a horizontal tab space |
\\ |
Backslash | Displays a backslash \ |
\" |
Double Quote | Displays a double quotation mark " |
\' |
Single Quote | Displays a single quotation mark ' |
\r |
Carriage Return | Moves the cursor to the beginning of the current line |
1. Newline – \n
What is \n?
The \n escape sequence is used to move the cursor to the next line.
It is similar to using endl.
Question
Write a C++ program that prints two lines of text using the newline escape sequence \n.
Example 1
#include <iostream>
using namespace std;
int main() {
cout << "Hello World!\n";
cout << "Welcome to C++.";
return 0;
}Output
Hello World!
Welcome to C++.Understand the Code
cout << "Hello World!\n";The \n moves the cursor to the next line. Therefore, the second cout statement starts on a new line.
Remember
\n → New line2. Tab – \t
What is \t?
The \t escape sequence inserts a horizontal tab space.
It is useful for creating simple columns in output.
Question
Write a C++ program that displays a list of items and their prices using \t to separate the columns.
Example 2
#include <iostream>
using namespace std;
int main() {
cout << "Item\tPrice\n";
cout << "Apple\t100\n";
cout << "Mango\t150\n";
return 0;
}Output
Item Price
Apple 100
Mango 150Understand the Code
cout << "Apple\t100\n";\tadds a tab space betweenAppleand100.\nmoves the cursor to the next line.
Remember
\t → Tab space3. Backslash – \\
What is \\?
The backslash character \ has a special meaning in C++.
If we want to display an actual backslash, we use two backslashes:
\\Question
Write a C++ program that displays a Windows file path containing backslashes.
Example 3
#include <iostream>
using namespace std;
int main() {
cout << "File path: C:\\Users\\Student\\Documents";
return 0;
}Output
File path: C:\Users\Student\DocumentsUnderstand the Code
In the program:
C:\\Users\\Student\\DocumentsEach \\ represents one actual backslash in the output.
Remember
\\ → Displays \4. Double Quote – \"
What is \"?
A double quotation mark " normally marks the beginning or end of a string.
If we want to display a double quotation mark inside a string, we use:
\"Question
Write a C++ program that prints a sentence containing double quotation marks.
Example 4
#include <iostream>
using namespace std;
int main() {
cout << "He said, \"C++ is fun!\"";
return 0;
}Output
He said, "C++ is fun!"Understand the Code
\"C++ is fun!\"The \" tells C++ to display a quotation mark instead of treating it as the end of the string.
Remember
\" → Displays "5. Single Quote – \'
What is \'?
The \' escape sequence can be used to display a single quotation mark.
Question
Write a C++ program that prints a character enclosed in single quotation marks.
Example 5
#include <iostream>
using namespac6. Carriage Return – \r
In C++, \r means carriage return. It moves the cursor to the beginning of the current line without moving to a new line.
Example 6
#include <iostream>
using namespace std;
int main() {
cout << "Hello\rWorld";
return 0;
}Understand the Code
-
Hellois printed on the screen. -
\rmoves the cursor to the beginning of the same line. -
Worldis printed from the beginning, overwriting the existing characters where applicable.
The output is typically:
WorldExample 7: Easy-to-Understand Demonstration
#include <iostream>
using namespace std;
int main() {
cout << "ABCDEF\rXYZ";
return 0;
}Output:
XYZDEFHere, \r returns the cursor to the beginning. XYZ replaces the first three characters, while DEF remains.
Remember the Difference
| Escape sequence | Meaning |
|---|---|
| \n | Moves to a new line |
| \r | Moves to the beginning of the current line |
| \t | Adds a horizontal tab |
Tip: The visible result of \r can vary depending on the console or terminal. It is often used to update progress messages on the same line.