Escape Sequences in C++

Characters such as backspace or control characters have no visible image and they are nonprintable. Other characters (like single or double quotation marks question marks and backslash) have special meanings in the programming language and you cannot use any of these characters directly. Instead, you need to use an escape sequence to represent such characters.


An Escape Sequence begins with backslash (\) followed by a character or by a combination of digits. 


There are several escape sequences are defined in C++ programming language such as:

Escape Sequence Character Represented
\n New Line
\t Horizontal Tab
\b Backspace
\" Double quote
\' Single quote
\? Question Mark
\r Carriage Return
\a Alert (bell)
\\ Backslash
\f Form feed (new page)

C++ Example of Escape Sequences.
//Example of C++ Escape Sequence
#include<iostream>
using namespace std;

int main()
{
  cout<<"This is an example of \'single quotes\' and \"double quotes\"";
  cout<<"\nThis is an example of newline and \\backslash";
  cout<<"\nThis is an example of \t Horizontal tab and a\bbackspace";
  
  return 0;
}
Output:
This is an example of 'single quotes' and "double quotes"
This is an example of newline and \backslash
This is an example of    Horizontal tab and backspace

Conclusion:

Escape sequences in C++ are special character combinations that are used to represent characters that are difficult or impossible to type directly in a string or character literal. They begin with a backslash (\) followed by a specific character or sequence of characters.

Next:

⚡ Please share your valuable feedback and suggestion in the comment section below or you can send us an email on our offical email id ✉ algolesson@gmail.com. You can also support our work by buying a cup of coffee ☕ for us.

Similar Posts

No comments:

Post a Comment


CLOSE ADS
CLOSE ADS