08.01.2023 Views

Learn to Program with C_ Learn to Program using the Popular C Programming Language ( PDFDrive )

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 6 ■ Characters

The ASCII codes run from 0 to 127 (the range of numbers that can be stored using 7 bits).

The ASCII character set is shown in Appendix B. Interesting features to note are the following:

• The digits 0 to 9 occupy codes 48 to 57.

• The uppercase letters A to Z occupy codes 65 to 90.

• The lowercase letters a to z occupy codes 97 to 122.

Note, however, that even though the ASCII set is defined using a 7-bit code, it is stored on

most computers in 8-bit bytes—a 0 is added at the front of the 7-bit code. For example, the 7-bit

ASCII code for A is 1000001; on a computer, it is stored as 01000001, occupying one byte.

In this book, as far as is possible, we will write our programs making no assumptions about

the underlying character set. Where it is unavoidable, we will assume that the ASCII character set

is used. For instance, we may need to assume that the uppercase letters are assigned consecutive

codes, similarly for lowercase letters. This may not necessarily be true for another character set.

Even so, we will not rely on the specific values of the codes, only that they are consecutive.

6.2 Character Constants and Values

A character constant is a single character enclosed in single quotes such as ‘A’, ‘+’ and ‘5’. Some

characters cannot be represented like this because we cannot type them. Others play a special

role in C (e.g. ', \). For these, we use an escape sequence enclosed in single quotes. Some examples

are shown in the following table:

char description Code

'\n' new line 10

'\f' form feed 12

'\t' tab 9

'\'' single quote 39

'\\' backslash 92

The character constant '\0' is special in C; it is the character whose code is 0, normally

referred to as the null character. One of its special uses is to indicate the end of a string in memory

(see Chapter 8).

The character value of a character constant is the character represented, without the single

quotes. Thus, the character value of 'T' is T and the character value of '\\' is \.

A character constant has an integer value associated with it—the numeric code of the character

represented. Thus, the integer value of 'T' is 84 since the ASCII code for T is 84. The integer value of

'\\' is 92 since the ASCII code for \ is 92. And the integer value of '\n' is 10 since the ASCII code

for the newline character is 10.

142

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!