Enumerated Types

I l @ ve RuBoard

Enumerated Types

You can use the enumerated type to declare symbolic names to represent integer constants. It is a common C extension that has been incorporated into the ANSI C standard. By using the enum keyword, you can create a new "type" and specify the values it may have. (Actually, enum constants are type int , so they can be used wherever you would use an int .) The purpose of enumerated types is to enhance the readability of a program. The syntax is similar to that used for structures. For example, you can make these declarations:

 enum spectrum {red, orange, yellow, green, blue, violet}; enum spectrum color; 

The first declaration establishes spectrum as a type name , and the second declaration makes color a variable of that type. The identifiers within the braces enumerate the possible values that a spectrum variable can have. Therefore, the possible values for color are red , orange , yellow , and so on. Then, you can use statements like the following:

 color = blue; if (color == yellow)    ...; for (color = red; color <= violet; color++)    ...; 

Although enumerated constants are type int , enumerated variables are more loosely constrained to be an integral type as long as the type can hold the enumerated constants. For example, the enumerated constants for spectrum have the range 0 “7, so a compiler could choose to use unsigned char to represent the color variable.

enum Constants

Just what are blue and red ? Technically, they are integer type constants. For instance, given the preceding enumeration declaration, you can try this:

 printf("red = %d, orange = %d\n", red, orange); 

Here is the output:

 red = 0, orange = 1 

What has happened is that red has become a named constant representing the integer 0. Similarly, the other identifiers are named constants representing the integers 1 through 5. The process is similar to using defined constants, except that these definitions are set up by the compiler rather than the preprocessor.

Default Values

By default, the constants in the enumeration list are assigned the integer values 0, 1, 2, and so on. Therefore, the declaration

 enum kids {nippy, slats, skippy, nina, liz}; 

results in nina having the value 3 .

Assigned Values

You can choose the integer values that you want the constants to have. Just include the desired values in the declaration:

 enum levels {low = 100, medium = 500, high = 2000}; 

If you assign a value to one constant but not to the following constants, the following constants will be numbered sequentially. For example, suppose you have this declaration:

 enum feline {cat, lynx = 10, puma, tiger}; 

Then cat is , by default, and lynx , puma , and tiger are 10 , 11 , and 12 , respectively.

Usage

Recall that the purpose of enumerated types is to enhance a program's readability. If you are dealing with colors, using red and blue is much more obvious than using and 1 . Note that the enumerated types are for internal use. If you want to enter a value of orange for color , you have to enter a 1 , not the word orange , or you can read in the string "orange" and have the program convert it to the value orange .

Because the enumerated type is an integer type, enum variables can be used in expressions in the same manner as integer variables. They make convenient labels for a case statement.

Listing 16.9 shows a short example using enum . The example relies on the default value-assignment scheme. This gives red the value , which makes it the index for the pointer to the string "red" .

Listing 16.9 The enum.c program.
 /* enum.c -- uses enumerated values */ #include <stdio.h> #include <string.h> enum BOOL {FALSE, TRUE}; typedef enum BOOL BOOLEAN; enum spectrum {red, orange, yellow, green, blue, violet}; const char * colors[] = {"red", "orange", "yellow",                          "green", "blue", "violet"}; #define LEN 30 int main(void) {    char choice[LEN];    enum spectrum color;    BOOLEAN found = FALSE;    puts("Enter a color (empty line to quit):");    while (gets(choice) != NULL && choice[0] != ' 
  /* enum.c -- uses enumerated values */ #include <stdio.h> #include <string.h> enum BOOL {FALSE, TRUE}; typedef enum BOOL BOOLEAN; enum spectrum {red, orange, yellow, green, blue, violet}; const char * colors[] = {"red", "orange", "yellow", "green", "blue", "violet"}; #define LEN 30 int main(void) { char choice[LEN]; enum spectrum color; BOOLEAN found = FALSE; puts("Enter a color (empty line to quit):"); while (gets(choice) != NULL && choice[0] != '\0') { for (color = red; color <= violet; color++) { if (strcmp(choice, colors[color]) == 0) { found = TRUE; break; } } if (found == TRUE) switch(color) { case red : puts("Roses are red."); break; case orange : puts("Poppies are orange."); break; case yellow : puts("Sunflowers are yellow."); break; case green : puts("Grass is green."); break; case blue : puts("Bluebells are blue."); break; case violet : puts("Violets are violet."); break; } else printf("I don't know about the color %s.\n", choice); found = FALSE; puts("Next color, please (empty line to quit:"); } return 0; }  
') { for (color = red; color <= violet; color++) { if (strcmp(choice, colors[color]) == 0) { found = TRUE; break; } } if (found == TRUE) switch(color) { case red : puts("Roses are red."); break; case orange : puts("Poppies are orange."); break; case yellow : puts("Sunflowers are yellow."); break; case green : puts("Grass is green."); break; case blue : puts("Bluebells are blue."); break; case violet : puts("Violets are violet."); break; } else printf("I don't know about the color %s.\n", choice); found = FALSE; puts("Next color, please (empty line to quit:"); } return 0; }

Note

Some compilers predefine FALSE and TRUE , so you would have to use, say, False and True instead in this enum definition.


Many programs use a single enum statement instead of several #define directives to create named constants.

I l @ ve RuBoard


C++ Primer Plus
C Primer Plus (5th Edition)
ISBN: 0672326965
EAN: 2147483647
Year: 2000
Pages: 314
Authors: Stephen Prata

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net