< previous page page_520 next page >

Page 520
If you answered E, you are right. The first statement declares ch and initializes it to the integer value 68 (assuming ASCII). The next statement increments ch to 69, and then its external representation (the letter E) is printed. Extending this idea of incrementing a char variable, we could print the letters A through G as follows:
char ch;

for (ch = A; ch <= G; ch++)
    cout < ch;
This code initializes ch to A (65 in ASCII). Each time through the loop, the external representation of ch is printed. On the final loop iteration, the G is printed and ch is incremented to H (72 in ASCII). The loop test is then false, so the loop terminates.
C++ char Constants
In C++, char constants come in two different forms. The first form, which we have been using regularly, is a single printable character enclosed by apostrophes (single quotes):
A 8 ) +
Notice that we said printable character. Character sets include both printable characters and control characters (or nonprintable characters). Control characters are not meant to be printed but are used to control the screen, printer, and other hardware devices. If you look at the ASCII character table, you see that the printable characters are those with integer values 32126. The remaining characters (with values 031 and 127) are nonprintable control characters. In the EBCDIC character set, the control characters are those with values 063 and 250255 (and some that are intermingled with the printable characters). One control character you already know about is the newline character, which causes the screen cursor to advance to the next line.
To accommodate control characters, C++ provides a second form of charconstant: the escape sequence. An escape sequence is one or more characters preceded by a backslash (\). You are familiar with the escape sequence \n, which represents the newline character. Here is the complete description of the two forms of char constant in C++:
1. A single printable characterexcept an apostrophe (') or backslash (\) enclosed by apostrophes.

 
< previous page page_520 next page >