Section 3 - Char Types
2B.3.1 char Type
A char object can hold a single alphanumeric character, like this:
char letter; letter = 's'; cout << "The letter is " << letter << endl;
results in:
Notice that the character 's' is contained within single quotes. This means that the computer is to store an 8-bit (byte) ASCII code representing the letter 's' in the char object letter. These codes are universal and can be found in many references, but you really don't need to know them, just that they exist.
Note that the statements
letter = '3';
and
x = 3; // assumes x is an int
are very different. The value stored in x is an actual numeric 3, whereas the value stored in letter is the ASCII code for the numeral '3' (which happens to be a number other than 3).
The char type can hold non-printable codes as well. The tab and newline keys have codes that we can use. For these special characters, we use notation like:
'\t' tab '\n' newline '\0' null or zero
Even though there are two characters between the single quotes, a slash plus an extra one, the value of the constant is still a single ASCII character. The last example above shows the constant for the char value = 0. This is different from the ASCII code for '0' whose value is not 0. Thus, '\0' stands for the byte with all bits turned off (set to 0).