Sams Teach Yourself C in 21 Days

(singke) #1
Working with Characters and Strings 227

10


ASCII code 191 is character ¿
ASCII code 192 is character À
ASCII code 193 is character Á
ASCII code 194 is character Â
ASCII code 195 is character Ã
ASCII code 196 is character Ä
ASCII code 197 is character Å
ASCII code 198 is character Æ
ASCII code 199 is character Ç
ASCII code 200 is character È
ASCII code 201 is character É
ASCII code 202 is character Ê
ASCII code 203 is character Ë
Looking at this program, you see that line 5 declares an unsignedcharacter vari-
able,mychar. This gives a range of 0 to 255. As with other numeric data types,
you must not initialize a charvariable to a value outside the allowed range, or you might
get unexpected results. In line 11,mycharis not initialized outside the range; instead, it is
initialized to 180. In the forstatement,mycharis incremented by 1 until it reaches 204.
Each time mycharis incremented, line 13 prints the value of mycharand the character
value of mychar. Remember that %cprints the character, or ASCII, value of mychar.

ANALYSIS

DOuse%cto print the character value of
a number.
DOuse single quotations when initializ-
ing a variable.
DOlook at the ASCII chart in Appendix A
to see the interesting characters that can
be printed.

DON’Tuse double quotations when ini-
tializing a character variable.
DON’Ttry to put extended ASCII charac-
ter values into a signed charvariable.

DO DON’T


Using Strings ......................................................................................................

Variables of type charcan hold only a single character, so they have limited usefulness.
You also need a way to store strings.A string is simply a sequences of characters. A per-
son’s name and address are examples of strings. Although there is no special data type
for strings, C handles this type of information with arrays of characters.

Some computer systems might use a different character set; however, most
Caution use the same ASCII values for 0 to 127.

17 448201x-CH10 8/13/02 11:17 AM Page 227

Free download pdf