Previous: 4.1 A New Data Type: char
Up: 4.1 A New Data Type: char
Next: 4.1.2 Operations on Characters
Previous Page: 4.1 A New Data Type: char
Next Page: 4.1.2 Operations on Characters

4.1.1 The ASCII Character Set

Character data is represented in a computer by using standardized numeric codes which have been developed. The most widely accepted code is called the American Standard Code for Information Interchange ( ASCII). The ASCII code associates an integer value for each symbol in the character set, such as letters, digits, punctuation marks, special characters, and control characters. Some implementations use other codes for representing characters, but we will use ASCII since it is the most widely used. The ASCII characters and their decimal code values are shown in Table 4.2. Of course, the internal machine representation of characters is in equivalent binary form.

The ASCII table has 128 characters, with values from 0 through 127. Thus, 7 bits are sufficient to represent a character in ASCII; however, most computers typically reserve 1 byte, (8 bits), for an ASCII character. One byte allows a numeric range from 0 through 255 which leaves room for growth in the size of the character set, or for a sign bit. Consequently, a character data type may optionally represent signed values; however, for now, we will assume that character data types are unsigned, i.e. positive integer values, in the range 0-127.

Looking at the table, note that the decimal values 0 through 31, and 127, represent non-printable control characters. All other characters can be printed by the computer, i.e. displayed on the screen or printed on printers, and are called printable characters. All printable characters and many control characters can be input to the computer by typing the corresponding keys on the keyboard. The character column shows the key(s) that must be pressed. Only a single key is pressed for a printable character; however, control characters need either special keys on the keyboard or require the CTRL key pressed together with another key. In the table, a control key is shown by the symbol '136. Thus, '136A is control-A, i.e. the CTRL key kept pressed while pressing the key, A.

Notice that the character 'A' has the code value of 65, 'B' has the value 66, and so on. The important feature is the fact that the ASCII values of letters 'A' through 'Z' are in a contiguous increasing numeric sequence. The values of the lower case letters 'a' through 'z' are also in a contiguous increasing sequence starting at the code value 97. Similarly, the digit symbol characters '0' through '9' are also in an increasing contiguous sequence starting at the code value 48. As we shall see, this feature of the ASCII code is quite useful.

It must be emphasized that a digit symbol is a character type. Digit characters have code values that differ from their numeric equivalents: the code value of '0' is 48, that of '1' is 49, that of '2' is 50, and so forth. The table shows that the character with code value 0 is a control character, '136@, called the NULL character. Do NOT confuse it with the digit symbol '0'. Remember, a digit character and the equivalent number have different representations.

Besides using single quotes, it is also possible to write character constants in terms of their ASCII values in a C program, using either their octal or their hexadecimal ASCII values. In writing character constants, the octal or hexadecimal value follows the escape character, , as shown in Table 4.3. At most three octal digits or at most two hexadecimal digits are needed. Note, after the escape backslash, a leading zero should not be included in writing octal or hexadecimal numbers.

The last example in Table 4.3, '0', is called the NULL character, whose ASCII value is zero. Once again, this is NOT the same character as the printable digit character, '0', whose ASCII value is 48.



Previous: 4.1 A New Data Type: char
Up: 4.1 A New Data Type: char
Next: 4.1.2 Operations on Characters
Previous Page: 4.1 A New Data Type: char
Next Page: 4.1.2 Operations on Characters

tep@wiliki.eng.hawaii.edu
Wed Aug 17 08:29:11 HST 1994