Previous: 4.6 Common Errors
Up: 4 Processing Character Data
Next: 4.8 Exercises
Previous Page: 4.6 Common Errors
Next Page: 4.8 Exercises

4.7 Summary

In this chapter we have introduced a new data type, char, used to represent textual data in the computer. Characters are represented using a standard encoding, or assignment of a bit pattern to each character in the set. This encoding is called ASCII and includes representations of several classes of characters such as alphabetic characters (letters, both upper and lower case), digit characters, punctuation, space, other special symbols, and control characters. We have seen how character variables can be declared using the char keyword as the type specifier in a declaration statement, and how character constants are expressed in the program, namely by enclosing them in single quotes, e.g. 'a'. The ASCII value of a character can be treated as an integer value, so we can do arithmetic operations using character variables and constants. For example, we have discussed how characters can be tested using relational operators to determine their class, how characters can be converted, for example from upper to lower case, or from a digit to its corresponding integer value.

We have also discussed character Input/Output using scanf() and printf() with the %c conversion specifier, or the getchar() and putchar() routines defined in stdio.h. We have used these routines and operations to write several example programs for processing characters and discussed the organization of program code into separate source files. This later technique allows us to develop our own libraries of utility functions which can be linked to various programs, further supporting our modular programming style.

In this chapter we have also introduced several new control constructs available in the C language. These include the switch statement:

where the <statement> is usually a compound statement with case labels. The semantics of this statement are that the <expression> is evaluated to an integer type value and the case labels are searched for the first label that matches this value. If no match is found, the optional default label is considered to match any value. Control flow transfers to the statement associated with this label and proceeds to successive statements in the switch body. We can control which statements are executed further by using return or break statements with the switch body.

The syntax of the break statement is simply:

and it may be used only within switch or loop bodies with the semantics of immediately terminating the execution of the body. In loops, the break statement is best used to terminate a loop under unusual or error conditions. A similar control construct available for loops is the continue statement:

which immediately terminates the current iteration of the loop but returns to the loop condition test to determine if the loop body is to be executed again.

We have also discussed some of the difficulties that can be encountered when mixing numeric and character data on input. These difficulties are due to the fact that numeric conversion specifiers ( %d or %f) are ``tolerant'' of white space, i.e. will skip leading white space in the input buffer to find numeric characters to be read and converted, while character input (using %c or getchar()) is not. For character input, the next character, whatever it is, is read. In addition, numeric conversions will stop at the first non-numeric character detected in the input, leaving it in the buffer. We have shown several ways of handling this behavior to make the input tolerant of user errors in Section 4.4.

Finally, we used the features of the language discussed in this chapter to implement a common style of user interface: menu driven programs. Such a style of program also facilitates good top down, modular design in the coding and testing of our programs.



Previous: 4.6 Common Errors
Up: 4 Processing Character Data
Next: 4.8 Exercises
Previous Page: 4.6 Common Errors
Next Page: 4.8 Exercises

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