为某些整数定义一个别名,可以利用预处理命令#define ,例如:
#define MON 1
#define TUE 2
#define WED 3
另一种新的数据类型,也能完成同样工作,枚举型
enum DAY
{
MON=1, TUE, WED, THU, FRI, SAT, SUN
};
枚举法的性质:
1)枚举型是一个集合,集合中的元素(枚举成员)是一些命名的整型常量,元素间逗号隔开。
2)DAY是一个标识符,可以看成集合的名字,(可有可无).
3)第一个枚举成员默认值为0,后续枚举成员的值在前一个成员上加1.
4)人为设定枚举成员的值,自定义某个范围内的整数。
5)枚举型是预处理指令#define的带替
6)类型定义分好" ; "结束。
1.用枚举类型对变量进行声明
1)枚举类型的定义和变量的声明分开。
enum DAY
{
MON=1, TUE, WED, THU, FRI, SAT, SUN
};
enum DAY yesterday;
enum DAY today;
enum DAY tomorrow; //变量tomorrow的类型为枚举型enum DAY
enum DAY good_day, bad_day; //变量good_day和bad_day的类型均为枚举型enum DAY
2)类型定义与变量声明同事进行
enum //跟第一个定义不同的是,此处的标号DAY省略,这是允许的。
{
saturday,
sunday = 0,
monday,
tuesday,
wednesday,
thursday,
friday
} workday; //变量workday的类型为枚举型enum DAY
enum week { Mon=1, Tue, Wed, Thu, Fri Sat, Sun} days; //变量days的类型为枚举型enum week
enum BOOLEAN { false, true } end_flag, match_flag; //定义枚举类型并声明了两个枚举型变量
3)用typedef关键字将枚举型定义成别名,并利用别名进行变量声明:
typedef enum workday
{
saturday,
sunday = 0,
monday,
tuesday,
wednesday,
thursday,
friday
} workday; //此处的workday为枚举型enum workday的别名
workday today, tomorrow; //变量today和tomorrow的类型为枚举型
workday,即enum workday
也可以:
typedef enum
{
saturday,
sunday = 0,
monday,
tuesday,
wednesday,
thursday,
friday
} workday; //此处的workday为枚举型enum workday的别名
workday today, tomorrow; //变量today和tomorrow的类型为枚举型workday,即enum workday
也可以用这种方式:
typedef enum workday
{
saturday,
sunday = 0,
monday,
tuesday,
wednesday,
thursday,
friday
};
workday today, tomorrow; //变量today和tomorrow的类型为枚举型workday,即enum workday
注意:同一个程序中,不能定义同名的枚举类型,不同的枚举类型中不能存在同名的命名常量
**错误声明一:存在同名的枚举类型**
typedef enum
{
wednesday,
thursday,
friday
} workday;
typedef enum WEEK
{
saturday,
sunday = 0,
monday,
} workday;
错误声明二:存在同名的枚举成员
typedef enum
{
wednesday,
thursday,
friday
} workday_1;
typedef enum WEEK
{
wednesday,
sunday = 0,
monday,
} workday_2;
3 对枚举类型的赋值与基本数据类型的赋值进行了对比。
1)先声明变量,在对变量赋值
#include<stdio.h>
enum DAY { MON=1, TUE, WED, THU, FRI, SAT, SUN };
void main()
{
int x, y, z;
x = 10;
y = 20;
z = 30;
enum DAY yesterday, today, tomorrow;
yesterday = MON;
today = TUE;
tomorrow = WED;
printf('%d %d %d ', yesterday, today, tomorrow);
}
方法二:声明变量的同事赋值
#include
enum DAY { MON=1, TUE, WED, THU, FRI, SAT, SUN };
void main()
{
int x=10, y=20, z=30;
enum DAY yesterday = MON,
today = TUE,
tomorrow = WED;
printf('%d %d %d ', yesterday, today, tomorrow);
方法三:定义类型的同时声明变量,然后对变量赋值
#include <stdio.h>
enum DAY { MON=1, TUE, WED, THU, FRI, SAT, SUN } yesterday, today, tomorrow;
int x, y, z;
void main()
{
x = 10; y = 20; z = 30;
yesterday = MON;
today = TUE;
tomorrow = WED;
printf('%d %d %d ', x, y, z); //输出:10 20 30
printf('%d %d %d ', yesterday, today, tomorrow); //输出:1 2 3
}
方法四:类型定义,变量声明,赋初值同时进行。
#include <stdio.h>
enum DAY
{
MON=1,
TUE,
WED,
THU,
FRI,
SAT,
SUN
}
yesterday = MON, today = TUE, tomorrow = WED;
int x = 10, y = 20, z = 30;
void main()
{
printf('%d %d %d ', x, y, z); //输出:10 20 30
printf('%d %d %d ', yesterday, today, tomorrow); //输出:1 2 3
}
4. 对枚举型的变量附整数值时,需进行类型转换。
#include
enum DAY { MON=1, TUE, WED, THU, FRI, SAT, SUN };
void main()
{
enum DAY yesterday, today, tomorrow;
yesterday = TUE;
today = (enum DAY) (yesterday + 1); //类型转换
tomorrow = (enum DAY) 30; //类型转换
//tomorrow = 3; //错误
printf(’%d %d %d ', yesterday, today, tomorrow); //输出:2 3 30
}