int main()
{ enum colorname{red,yellow,blue,white,black};
enum colorname color;
for(color=red;color<black;color++)
switch(color)
{ case red:printf(″red″);break;
case yellow:printf(″yellow″);break;
case blue:printf(″blue″);break;
case white:printf(″white″);break;
case black:printf(″black″);break;
}
return0;
}
例2 两个枚举类型综合使用
#include<stdio.h>enum Season
{
spring, summer=100, fall=96,winter //在定义枚举类型时改变枚举元素的值
};
typedefenum//类型重定义
{
Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday
} Weekday;
void main()
{
/* Season */printf("%d \n", spring); // 0 printf("%d, %c \n", summer, summer); // 100, d printf("%d \n", fall+winter); // 193
Season mySeason=winter;
if(winter==mySeason)
printf("mySeason is winter \n"); // mySeason is winter int x=100;
if(x==summer)
printf("x is equal to summer\n"); // x is equal to summer printf("%d bytes\n", sizeof(spring)); // 4 bytes /* Weekday */printf("sizeof Weekday is: %d \n", sizeof(Weekday)); //sizeof Weekday is: 4
Weekday today = Saturday;
Weekday tomorrow;
if(today == Monday)
tomorrow = Tuesday;
else
tomorrow = (Weekday) (today + 1); //转换
}