Learn C++ - C++ char Type

char type is designed to store characters, such as letters and numeric digits.

The most common symbol set is the ASCII character set.

For example, 65 is the code for the character A, and 77 is the code for the character M.

Try the char type in the following code.

#include <iostream> 
int main( ) 
     using namespace std; 
     char ch;        // declare a char variable 

     cout << "Enter a character: " << endl; 
     cin >> ch; 
     cout << "hi! "; 
     cout << "Thank you for the " << ch << " character." << endl; 
     return 0; 


The following code illustrates the char type and int type contrasted.

#include <iostream> 
int main() /* ww  w . j av  a2s  . c o  m*/
    using namespace std; 
    char ch = 'M';       // assign ASCII code for M to ch 
    int i = ch;          // store same code in an int 
    cout << "The ASCII code for " << ch << " is " << i << endl; 

    cout << "Add one to the character code:" << endl; 
    ch = ch + 1;          // change character code in ch 
    i = ch;               // save new character code in i 
    cout << "The ASCII code for " << ch << " is " << i << endl; 


    // using cout.put() to display a char constant 

    cout << endl << "Done" << endl; 
    return 0; 

The code above generates the following result.

char Literals

You have several options for writing character literals in C++.

We can write ordinary characters, such as letters, punctuation, and digits, is to enclose the character in single quotation marks.

This notation stands for the numeric code for the character.

For example, an ASCII system has the following correspondences:

'A'is 65, the ASCII code for A. 'a'is 97, the ASCII code for a.

Escape Sequence

C++ has special notations, called escape sequences, as shown in the following Table.

Character NameASCII SymbolC++ CodeASCII DecimalASCII Hex Code
NewlineNL (LF)\n100xA
Horizontal tabHT\t90x9
Vertical tabVT\v110xB
Carriage returnCR\r130xD
Question mark?\?630x3F
Single quote'\'390x27
Double quote"\"340x22

For example, \a represents the alert character, which beeps your terminal's speaker or rings its bell.

The escape sequence \n represents a newline.

And \" represents the double quotation mark as an ordinary character instead of a string delimiter.

char alarm = '\a'; 
cout << alarm << "this is a test!\a\n"; 
cout << "Java \"hi \" C++\n was here!\n"; 

The newline character provides an alternative to endl for inserting new lines.

All three of the following move the screen cursor to the beginning of the next line:

cout << endl;    // using the endl manipulator 
cout << '\n';    // using a character constant 
cout << "\n";    // using a string 

You can use escape sequences based on the octal or hexadecimal codes for a character.

For example, Ctrl+Z has an ASCII code of 26, which is 032 in octal and 0x1a in hexadecimal.

You can represent this character with either of the following escape sequences: \032 or \x1a.

The following code demonstrates a few escape sequences.

#include <iostream> 
int main() { /*  ww w  . jav a  2 s  .c om*/
     using namespace std; 
     cout << "\ahi \"hey\" is now activated!\n"; 
     cout << "Enter your agent code:________\b\b\b\b\b\b\b\b"; 
     long code; 
     cin >> code; 
     cout << "\aYou entered " << code << "...\n"; 
     cout << "\ahi! !\n"; 
     return 0; 

The code above generates the following result.