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
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:
The syntax of the break statement is simply:
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.