Open In App

C++ Escape Sequences

Last Updated : 03 Jun, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Escape sequences in C++ are characters or sequences of characters that can be used inside a string literal to represent some special characters. They are prefixed with a backslash \ and are used to represent characters such as new line, carriage return, etc. that cannot be represented normally.

In this article, we will learn about escape characters in C++ and how to use them in our C++ program.

Escape Sequences in C++

The following table lists the commonly used escape characters in C++:

Escape SequenceName

Description

\\

Backslash

Inserts a backslash character.

\'

Single quote

Displays a single quotation mark.

\"

Double quote

Displays double quotation marks.

\?

Question mark

Displays a question mark.

\a

Alert (bell)

Generates a bell sound in the C++ program.

\b

Backspace

Moves the cursor one place backward.

\f

Form feed

Moves the cursor to the start of the next logical page.

\n

New line

Moves the cursor to the start of the next line.

\r

Carriage return

Moves the cursor to the start of the current line.

\t

Horizontal tab

Inserts some whitespace to the left of the cursor and moves the cursor accordingly.

\v

Vertical tab

Inserts vertical space.

\0

Null character

Represents the NULL character.

\ooo

Octal number

Represents an octal number.

\xhh

Hexadecimal number

Represents a hexadecimal number.

Note: The output of some of these escape sequences depends upon the compiler you are working on.

Examples of Escape Characters in C++

The following examples demonstrates the use of above escape sequences in C++:

Example 1: Program to demonstrate how to use \a escape sequence in C++

C++
#include <iostream>
using namespace std;

int main()
{
    // Generates a bell sound
    cout << "My mobile number is "
            "7\a8\a7\a3\a9\a2\a3\a4\a0\a8\a";
    return 0;
}

Output
My mobile number is 7873923408


Example 2: Program to demonstrate how to use \b escape sequence in C++

C++
#include <iostream>
using namespace std;

int main()
{
    // Moves the cursor one place backward
    cout << "Hello \b\b\b\b\b\bHi Geeks";
    return 0;
}

Output
Hello Hi Geeks

Example 3: Program to demonstrate how to use \n escape sequence in C++

C++
#include <iostream>
using namespace std;

int main()
{
    // Moves the cursor to the start of the next line
    cout << "Hello\n";
    cout << "GeeksforGeeks";
    return 0;
}

Output
Hello
GeeksforGeeks

Example 4: Program to demonstrate how to use \t escape sequence in C++

C++
#include <iostream>
using namespace std;

int main()
{
    // Inserts a horizontal tab space
    cout << "Hello \t GFG";
    return 0;
}

Output
Hello      GFG

Example 5: Program to demonstrate how to use \v escape sequence in C++

C++
#include <iostream>
using namespace std;

int main()
{
    // Inserts a vertical tab space
    cout << "Hello friends\v";
    cout << "Welcome to GFG";
    return 0;
}

Output
Hello friendsWelcome to GFG

Example 6: Program to demonstrate how to use \r escape sequence in C++

C++
#include <iostream>
using namespace std;

int main()
{
    // Moves the cursor to the start of the current line
    cout << "Hello   Geeks \rGeeksfor";
    return 0;
}

Output
Hello   Geeks 
Geeksfor

Example 7: Program to demonstrate how to use \\ escape sequence in C++

C++
#include <iostream>
using namespace std;

int main()
{
    // Inserts a backslash character
    cout << "Hello\\GFG";
    return 0;
}

Output
Hello\GFG

Example 8: Program to demonstrate how to use \' and \" escape sequences in C++

C++
#include <iostream>
using namespace std;

int main()
{
    // Displays a single quotation mark
    cout << "\' Hello Geeks\n";
    // Displays double quotation marks
    cout << "\" Hello Geeks";
    return 0;
}

Output
' Hello Geeks
" Hello Geeks

Example 9: Program to demonstrate how to use \? escape sequence in C++

C++
#include <iostream>
using namespace std;

int main()
{
    // Displays a question mark
    cout << "\?\?!\n";
    return 0;
}

Output
??!

Example 10: Program to demonstrate how to use \ooo escape sequence in C++

C++
#include <iostream>
using namespace std;

int main()
{
    // Represents an octal number
    char* s = "A\072\065";
    cout << s;
    return 0;
}

Output
A:5

Example 11: Program to demonstrate how to use \xhh escape sequence in C++

C++
#include <iostream>
using namespace std;

int main()
{
    // Represents a hexadecimal number
    char* s = "B\x4a";
    cout << s;
    return 0;
}

Output
BJ

Conclusion

These sequences allow you to represent characters that cannot be directly typed, such as newlines, tabs, and backslashes. Knowing how to use escape sequences enhances your ability to handle text and improves the readability and functionality of your code.


Next Article
Practice Tags :

Similar Reads