| I l @ ve RuBoard |
You can use the
enumerated type
to declare symbolic
enum spectrum {red, orange, yellow, green, blue, violet};
enum spectrum color;
The first declaration establishes
spectrum
as a type
color = blue; if (color == yellow) ...; for (color = red; color <= violet; color++) ...;
Although enumerated constants are type
int
, enumerated
Just what are
blue
and
red
? Technically, they are integer type constants. For instance, given the
printf("red = %d, orange = %d\n", red, orange);
Here is the output:
red = 0, orange = 1
What has
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 .
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.
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
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" .
/* 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 |