Home Segments Index Top Previous Next

527: Mainline

There is a better way to use character codes in switch statements, however. You can change the values of the type-defining enumeration constants. Previously, in Chapter 27, they were declared as follows, with C assigning food to 0, trucking to 1, and so on, by default:

enum {food, trucking, computers, metals, health, airline}; 

Recall, however, that you can supply particular integers instead. Further recall that C views characters as an integer data type. Thus, you can declare the type-defining enumeration constants as follows:

enum {food = 'F',  
      trucking = 'T', 
      computers = 'C',  
      metals = 'M',  
      health = 'H',  
      airline = 'A'};