例:
#include <stdio.h>
const int red = 0;
const int yellow = 1;
const int green = 2;
等价于
#include <stdio.h>
enum COLOR{RED, YELLOW, GREEN};
用枚举而不是定义独立的const int 变量
#枚举
1.枚举是一种用户定义的数据类型,他用关键字enum以如下语法来声明:
enum 枚举类型名字{名字0,…,名字n};
2.枚举类型名字通常并不真的使用,要用的是大括号里的名字,因为他们就是常量符号,他们的类型是int,值依次从0到n。如:
enum color{red, green, yellow};
3.
@枚举量可以作为值
@枚举类型可以跟上enum作为类型
@但是实际上是以整数来做内部计算和外部输入输出的
样例代码
#include <stdio.h>
enum color{red, yellow, green};
void f(enum color c);
int main()
{
enum color t=red;//int t = 0;
scanf("%d",&t);
f(t);
return 0;
}
void f(enum color c)
{
printf("%d\n",c);
}
*注释:enum color 相当于int