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;
cout.put(ch);
// using cout.put() to display a char constant
cout.put('!');
cout << endl << "Done" << endl;
return 0;
}
The code above generates the following result.
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.
C++ has special notations, called escape sequences, as shown in the following Table.
Character Name | ASCII Symbol | C++ Code | ASCII Decimal | ASCII Hex Code |
---|---|---|---|---|
Newline | NL (LF) | \n | 10 | 0xA |
Horizontal tab | HT | \t | 9 | 0x9 |
Vertical tab | VT | \v | 11 | 0xB |
Backspace | BS | \b | 8 | 0x8 |
Carriage return | CR | \r | 13 | 0xD |
Alert | BEL | \a | 7 | 0x7 |
Backslash | \ | \\ | 92 | 0x5C |
Question mark | ? | \? | 63 | 0x3F |
Single quote | ' | \' | 39 | 0x27 |
Double quote | " | \" | 34 | 0x22 |
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.