1:枚举里面的值是常量; 例如 enum color {red,orange,yellow};
2:枚举列表中的常量默认为0,1,2等等 例如 enum color {red,orange,yellow};常量值默认为0,1,2
3: 枚举列表常量值是可以指定的,且指定值后的枚举值将递增例如enum color {red,orange=10,yellow};yellow=11
4: 在C语言中枚举可以递增运算符++,而C++中需要强制转换定义为int再运算符++
enum用法例子如下
#include<stdio.h>
#include<string.h>
enumspecturm {red,orange,yellow,green,blue,violet};
constchar*colors[]={"red","orange","yellow","green","blue","violet"};
#defineLEN 30
intmain(void)
{
charchoice[LEN];
intcolor;
intcolor_is_found=0;
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)
{
color_is_found=1;
break;
}
}
if(color_is_found)
switch(color)
{
case0:puts("Roses are red");break;
case1:puts("Poppies are orange");break;
case2:puts("Sunflowers are yellow");break;
case3: puts("Grass is green");break;
case4:puts("Bluebells are blue");break;
case5:puts("Violets are violet");break;
}
elseprintf("I don't know about the color %s.\n",choice);
color_is_found=0;
puts("Next color,please(empty line to quit):");
}
puts("Goodbye!");
return0;
}原文链接: https://www.cnblogs.com/xiaobaigang/archive/2011/05/27/2059845.html
欢迎关注
微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍
原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/26281
非原创文章文中已经注明原地址,如有侵权,联系删除
关注公众号【高性能架构探索】,第一时间获取最新文章
转载文章受原作者版权保护。转载请注明原作者出处!