c枚举 enum
测试完才发现我下面代码写的不规范,那个枚举里面的几种类型必须为全部大写字母
1、非赋值的枚举类型
[html]
#include "stdio.h"
enum weekday
{ sun,mon,tue,wed,thu,fri,sat } a,b,c;
main(){
a=sun;
b=mon;
c=tue;
enum weekday d ,e ,f;
d = wed;
e = thu;
f = fri;
printf("%d,%d,%d,%d,%d,%d\n",a,b,c,d,e,f);
}
输出:
[html]
pateo@pateo-B86N53X:~/work/study$ cc main.c -o main
pateo@pateo-B86N53X:~/work/study$ ./main
0,1,2,3,4,5
2、赋值的起始枚举
[html]
#include "stdio.h"
enum weekday
{ sun=0x0009,mon,tue,wed,thu,fri,sat } a,b,c;
main(){
a=sun;
b=mon;
c=tue;
enum weekday d ,e ,f;
d = wed;
e = thu;
f = fri;
printf("%d,%d,%d,%d,%d,%d\n",a,b,c,d,e,f);
}
输出:
[html]
pateo@pateo-B86N53X:~/work/study$ cc main.c -o main
pateo@pateo-B86N53X:~/work/study$ ./main
9,10,11,12,13,14
3、赋值特殊
[html
#include "stdio.h"
enum weekday
{ sun=0x0009,mon,tue,wed=0x0003,thu,fri,sat } a,b,c;
main(){
a=sun;
b=mon;
c=tue;
enum weekday d ,e ,f;
d = wed;
e = thu;
f = fri;
printf("%d,%d,%d,%d,%d,%d\n",a,b,c,d,e,f);
}
输出:
[html]
pateo@pateo-B86N53X:~/work/study$ cc main.c -o main
pateo@pateo-B86N53X:~/work/study$ ./main
9,10,11,3,4,5
4、重复特殊
[html]
#include "stdio.h"
enum weekday
{ sun=0x0009,mon=5,tue,wed=0x0003,thu=7,fri=2,sat=9 } a,b,c;
main(){
a=sun;
b=mon;
c=tue;
enum weekday d ,e ,f ,g;
d = wed;
e = thu;
f = fri;
g = sat;
printf("%d,%d,%d,%d,%d,%d,%d\n",a,b,c,d,e,f,g);
}
输出:
[html]
pateo@pateo-B86N53X:~/work/study$ cc main.c -o main
pateo@pateo-B86N53X:~/work/study$ ./main
9,5,6,3,7,2,9
补充:软件开发 , C语言 ,