Special Characters in C++

Written by

Vaaruni Agarwal

C++ allows us the use of some special characters, these special characters are:

  • Trigraphs
  • Escape Sequences

These special characters help in improving the coding practice for the coders and at the same time make the output more readable. So, let us take a look at these Special Characters in C++.

Trigraphs in C++:

There are a few characters that have an alternative representation in the C++ language, such characters are called the Trigraphs, and the representation so formed is known as the Trigraph Sequence.

It is called so because these characters are represented in three character sequences and the three character sequence always starts with two question marks.

Trigraphs are expanded anywhere they appear, including within string literals and character literals, in comments, and in preprocessor directives as well.

Let us take a look at some of the Trigraph Sequences and their Expansions in C++.

 

Trigraph  Expansion
??= 
??/ 
??’ 
??( 
??) 
??! 
??< 
??> 
??- 

 

Escape Sequences in C++:

Escape Sequences are used to represent some special characters in C++. So, if anyone wants to add a special character in C++, or if you want to add some extra formatting on the output screen then these escape sequences will be used.

Escape Sequences start with a backslash and then there is a unique character that tells what that sequence will do.

Escape sequence  Meaning 
\\  \ character 
\’  ‘ character 
\”  ” character 
\?  ? character 
\a  Alert or bell 
\b  Backspace 
\f  Form feed 
\n  Newline 
\r  Carriage return 
\t  Horizontal tab 
\v  Vertical tab 
\ooo  Octal number of one to three digits 
\xhh . . .  Hexadecimal number of one or more digits 

 

Following is a program depicting the use of some escape sequences in C++:

#include <iostream> 

void main() 

cout << "Hello\nWorld\nThis\tis C++\n"; 

return 0; 

}

Output:

Hello

World

This is C++

Here, \n and \t are the two escape sequences used and their effect is seen on the output screen.

 

Special Characters in C++