|
|
|
|
|
|
|
Assuming our machine uses the ASCII character set, the compiler translates the constant A into the integer 65. We could also have written the statement as |
|
|
|
|
|
|
|
|
Both statements have exactly the same effectthat of storing 65 into someChar. However, the second version is not recommended. It is not as understandable as the first version, and it is nonportable (the program won't work correctly on a machine that uses EBCDIC, which uses a different internal representation193for A). |
|
|
|
|
|
|
|
|
Earlier we mentioned that the computer cannot tell the difference between character and integer data in memory. Both are stored internally as integers. However, when we perform I/O operations, the computer does the right thingit uses the external representation that corresponds to the data type of the expression being printed. Look at this code segment, for example: |
|
|
|
|
|
|
|
|
// This example assumes use of the ASCII character set
int someInt = 97;
char someChar = 97;
cout < someInt < endl;
cout < someChar < endl; |
|
|
|
|
|
|
|
|
When these statements are executed, the output is |
|
|
|
|
|
|
|
|
When the < operator outputs someInt, it prints the sequence of characters 9 and 7. To output someChar, it prints the single character a. Even though both variables contain the value 97 internally, the data type of each variable determines how it is printed. |
|
|
|
|
|
|
|
|
What do you think will be output by the following sequence of statements? |
|
|
|
|
|
|
|
|
char ch = D;
ch++;
cout<< ch; |
|
|
|
|
|