C language enum enumeration type Detailed

enum

We enumeration can be understood as a compilation phase of macros, using the format:

enum typeName{ valueName1, valueName2, valueName3, ...... };

typeNameIs an enumerated type names , braces elements inside (enum member) is constant rather than a variable, this must be clear, because enum member is constant, it can not be assigned to them, their values can only be assigned to other variables

The difference between enum type (enumeration tag), enum member (enumeration constants), enumeration

Enumeration enum type and when put together defining, enumerated type name (that is, in the week enum week) can be omitted, meaning that there are two on FIG wording

The first:

enum week{Mon = 1, Tues, Wed, Thurs}num;

The second:

enum {Mon = 1, Tues, Wed, Thurs}num;

This fact, and the structure of unknown structure is very similar, although the definition of the enumeration type variables and define the structure type variables look similar, but the difference between the two is still quite large, a structure type variable which contains a number of members, It corresponds to a packaged delivery box,

The enumeration type variable is not the same, is not an enumerated type variable contains a collection of several members, enumerated types and variables int, char type of variable almost the same, but the enumeration type variable assignment can only use their own enumeration members be assigned to the above example,

num assignment can only use the enumeration member Mon, Tues, Wed, Thurs, and not with members of other enumerated type enumeration to assign

 

Same variable definition enumeration and definition of the structure, there are two defined methods

The first :( defined enumerated type, while the definition of enumeration)

enum week{Mon = 1, Tues, Wed, Thurs}num;

第二种:(先定义枚举类型,再定义枚举变量)

enum week{Mon = 1, Tues, Wed, Thurs};
enum week num;

枚举类型的特点

下面将用不同的示例来说明枚举类型的特点

示例一:(不显式说明枚举常量的值)

enum week{Mon, Tues, Wed, Thurs, Fri, Sat, Sun};

在没有显示说明的情况下,枚举常量(也就是花括号中的常量名)默认第一个枚举常量的值为0,往后每个枚举常量依次递增1,所以Mon=0,Tues=1,····Sun=6,下面来验证一下

enum week{Mon, Tues, Wed, Thurs, Fri, Sat, Sun};
printf("Mon=%d\nTues=%d\nWed=%d\nThurs=%d\nFri=%d\nSat=%d\nSun=%d\n", Mon, Tues, Wed, Thurs, Fri, Sat, Sun);

运行结果:

示例二:(显示说明部分枚举常量的值)

enum week{Mon = 1, Tues, Wed, Thurs, Fri, Sat, Sun};

上面的代码中,只显式说明了枚举常量Mon的值为1,未指定的枚举名的值将依着最后一个指定值向后依次递增(注意是最后一个指定值)

enum week{Mon = 1, Tues, Wed, Thurs, Fri, Sat, Sun};
printf("Mon=%d\nTues=%d\nWed=%d\nThurs=%d\nFri=%d\nSat=%d\nSun=%d\n", Mon, Tues, Wed, Thurs, Fri, Sat, Sun);

运行结果:

对上面的代码稍加修改

enum week{Mon = 1, Tues, Wed, Thurs, Fri = 10, Sat, Sun};
printf("Mon=%d\nTues=%d\nWed=%d\nThurs=%d\nFri=%d\nSat=%d\nSun=%d\n", Mon, Tues, Wed, Thurs, Fri, Sat, Sun);

运行结果:

示例三:(对枚举变量进行赋值)

enum week{Mon = 1, Tues, Wed, Thurs}num;

num = (enum week)10;
printf("%d", num);

一个整数不能直接赋值给一个枚举变量,必须用该枚举变量所属的枚举类型进行类型强制转换后才能赋值上面的代码中,在对枚举变量赋值10的时候进行的强制类型转换,将整型常量10转换成了enum week类型再赋值给num变量,

运行结果:

总结:

1、在没有显示说明的情况下,枚举常量(也就是花括号中的常量名)默认第一个枚举常量的值为0,往后每个枚举常量依次递增1

2、在部分显示说明的情况下,未指定的枚举名的值将依着之前最有一个指定值向后依次递增

3、一个整数不能直接赋值给一个枚举变量,必须用该枚举变量所属的枚举类型进行类型强制转换后才能赋值

4、同一枚举类型中不同的枚举成员可以具有相同的值

5、同一个程序中不能定义同名的枚��类型,不同的枚举类型中也不能存在同名的枚举成员(枚举常量)

enum与typedef配合使用

typedef enum week my;

或者

typedef enum week{Mon = 1, Tues, Wed, Thurs}my;

上面typedef也可以省略week

typedef enum{Mon = 1, Tues, Wed, Thurs}my;

上面两种形式中,my都是enum week的别名(意思是my和enum week等价),需要注意的是如果将第二句的typedef去掉那么my就变成了enum week类型的变量,如果加上typedef那么my就是enum的别名,这点要注意区分

在定义了enum week的别名my后,后面就可以使用my来定义枚举类型变量

my num;

num = (enum week)10;
printf("%d", num);

上述语句中的(enum week),可以改为(my),两者效果是一样的

补充:对上面总结的第五点:同一个程序中不能定义同名的枚举类型,不同的枚举类型中也不能存在同名的枚举成员(枚举常量)进行验证

enum m1{m1, m2};
enum m2{m3, m4};
enum m1{m5, m6};

上述代码定义的枚举类型enum m1多次定义,是不允许的,编译器会报错还有一种特殊情况,即使定义了同名的枚举类型,编译器也不会报错

enum m1{m1, m2};
enum m2{m3, m4};
enum m1;

上面的代码中,第二个enum m1没有定义枚举成员,在编译阶段是不会报错的(估计是编译器并不认为定义了枚举成员的enum m1 和 没有定义枚举成员的enum m1是同名的枚举类型

enum的赋值范围

这个也是无意间注意到的,文章在开头提到枚举类型描述的是一组整型值的集合,枚举类型的占的字节数是4个字节,所以我们可以用INT_MAX和INT_MIN来测试枚举类型的赋值范围,赋值的最大值应该是INT_MAX(也就是2147483647),下面用代码来测试一下

enum test{m1=INT_MAX};
printf("%d\n%d\n", m1, INT_MAX);

运行结果:

 

上面的代码没有问题,下面来测试一下越界的赋值

enum test{m1=INT_MAX+1};
printf("%d\n%d\n", m1, INT_MAX);

 编译器在编译阶段会有警告测试了枚举类型的最大值,下面来测试下枚举类型的最小值

enum test{m1=INT_MIN};
printf("%d\n%d\n", m1, INT_MIN);

运行结果:

上面的代码没有问题,下面来测试一下越界的赋值

enum test{m1=INT_MIN-1};
printf("%d\n%d\n", m1, INT_MIN);

和上面相同,编译器也会有警告

为什么“枚举类型描述的是一组整型值的集合”其实不太妥当?

上面这句话很容易让读者以为enum类型和int类型是等价的,好像可以把int型的数据直接赋值给enum变量,实际上是不行的,需要进行强制类型转换,C语言提供了一种称为“枚举”的类型,其中一个枚举常量的占的字节数为4个字节,恰好和int类型的变量占的字节数相同,假设把这些枚举常量说明为整型,字符型或其他类型显然是不妥当的,因为枚举类型是一种基本数据类型,而不是一种构造类型,它不能再分解为什么基本类型。

Guess you like

Origin www.linuxidc.com/Linux/2019-08/159760.htm