c语言枚举类型举例(C language enumeration type example).doc

上传人:rrsccc 文档编号:11040179 上传时间:2021-06-20 格式:DOC 页数:21 大小:41KB
返回 下载 相关 举报
c语言枚举类型举例(C language enumeration type example).doc_第1页
第1页 / 共21页
c语言枚举类型举例(C language enumeration type example).doc_第2页
第2页 / 共21页
c语言枚举类型举例(C language enumeration type example).doc_第3页
第3页 / 共21页
c语言枚举类型举例(C language enumeration type example).doc_第4页
第4页 / 共21页
c语言枚举类型举例(C language enumeration type example).doc_第5页
第5页 / 共21页
点击查看更多>>
资源描述

《c语言枚举类型举例(C language enumeration type example).doc》由会员分享,可在线阅读,更多相关《c语言枚举类型举例(C language enumeration type example).doc(21页珍藏版)》请在三一文库上搜索。

1、c语言枚举类型举例(C language enumeration type example)Note: the execution environment for all of the following code is VC+ 6The difference between macros and enumerationsThe main difference between macro and enumeration in different periods and stored in the form of action, is to replace the macro work in t

2、he pretreatment stage of the text, it replaced the code, running process - no longer exists. Enumeration does not work until the program is running. Enumeration constants are stored in the static storage section of the data segment. Macros take up space in the code segment, and enumerations consume

3、CPU resources in addition to space occupation.But also can not say good than if you need to enumerate the macro definition is very much, with a constant, enum is more obvious than. . A lot of define more clearly, the enumeration is also used to define some special types, such as Bool,Such as: type,

4、enum, FALSE, TRUE, Bool;In the program, you may need to define an alias for some integers. We can do this by using the preprocessing instruction #define. Your code might be:#define MON 1#define TUE 2#define WED 3#define THU 4#define FRI 5#define SAT 6#define SUN 7Here, we define a new data type that

5、 we hope will do the same. This new data type is called enumeration.1. define a new data type - enumeration typeThe following code defines this new data type - enumeration typeEnum DAYMON=1, TUE, WED, THU, FRI, SAT, SUN;(1) an enumerated type is a collection, and the elements in the collection (enum

6、eration members) are named integer constants, separated by commas.(2) DAY is an identifier that can be considered as the name of the collection, and is an optional, optional item.(3) the default value of the first enumerator member is 0 of the integer, and the value of the subsequent enumeration mem

7、ber is 1 on the previous member.(4) you can set the values of enumerated members artificially so that you can customize an integer in a range.(5) enumerated type is an alternative to the preprocessing instruction #define.(6) the type definition ends with a semicolon.2. declare variables using enumer

8、ated typesOnce the new data type definition is complete, it can be used. Weve already seen the most basic data types, such as integer int, single precision floating point float, double precision floating point double, character char, short integer short, and so on. Declaring variables with these bas

9、ic data types is usually the case:Char a; / / a type variables are character charChar letter;Int x,Y,Z; / / variable x, type Y and Z are integer intInt number;Double, m, n;Double result; / / result type variables for double precision floating-point doubleSince enumeration is also a data type, it can

10、 declare variables as well as the basic data types.Method 1: the definition of enumerated types is separated from the declarations of variablesEnum DAYMON=1, TUE, WED, THU, FRI, SAT, SUN;Enum DAY yesterday;Enum DAY today;Enum DAY tomorrow; / / the type of the tomorrow variable for the enumeration ty

11、pe enum DAYEnum DAY good_day, bad_day; / / type variables good_day and bad_day are enumerated enum DAYMethod two: type definitions and variable declarations are performed simultaneously:Enum / / different with the first definition is that here label DAY is omitted, it is allowed.Saturday,Sunday = 0,

12、Monday,Tuesday,Wednesday,Thursday,FridayWorkday; / / the type of the workday variable for the enumeration type enum DAYEnum week Mon=1, Tue, Wed, Thu, Fri Sat, Sun days; / / the type of the days variable for the enumeration type enum weekEnum BOOLEAN false, true end_flag, match_flag; / / define enum

13、erations and declares two enumeration type variablesMethod three: define the enumerated type as an alias with the typedef keyword and declare the variable using the alias:Typedef enum workdaySaturday,Sunday = 0,Monday,Tuesday,Wednesday,Thursday,FridayWorkday / / workday; here is an alias for workday

14、 enumeration type enumWorkday today, tomorrow today and tomorrow; / / type variables for the enumeration of the type workday, namely enum workdayThe workday in enum workday can be omitted:Typedef enum星期六,星期日= 0,星期一,星期二,星期三,星期四,星期五日;/工作日工作日的别名此处的为枚举型枚举工作日的今天,明天;/ /变量今天和明天的类型为枚举型工作日,工作日也即枚举也可以用这种方式:枚举

15、类型的声明的工作日星期六,星期日= 0,星期一,星期二,星期三,星期四,星期五;工作日的今天,明天;/ /变量今天和明天的类型为枚举型工作日,工作日也即枚举注意:同一个程序中不能定义同名的枚举类型,不同的枚举类型中也不能存在同名的命名常量。错误示例如下所示:错误声明一:存在同名的枚举类型枚举类型的声明星期三,星期四,星期五日;枚举类型的声明周星期六,星期日= 0,星期一,日;错误声明二:存在同名的枚举成员枚举类型的声明星期三,星期四,星期五 workday_1;枚举类型的声明周星期三,星期日= 0,星期一, workday_2;三.使用枚举类型的变量3.1对枚举型的变量赋值。实例将枚举类型的赋

16、值与基本数据类型的赋值进行了对比:方法一:先声明变量,再对变量赋值#包括枚举天 MON = 1,周二,周三,周四,周五,坐,太阳;无效main()int,y,z;x10;y20;z30;昨天今天明天枚举;昨天=星期一;今天是星期二;明天=结婚;printf(“%d %d %dn”,昨天,今天,明天);方法二:声明变量的同时赋初值#包括枚举天 MON = 1,周二,周三,周四,周五,坐,太阳;无效main()int10,y20,z30;昨天一天=一枚举,今天是星期二,明天=结婚;printf(“%d %d %dn”,昨天,今天,明天);方法三:定义类型的同时声明变量,然后对变量赋值。#包括枚举天

17、 MON = 1,周二,周三,周四,周五,坐,太阳昨天,今天,明天;int,y,z;无效main()x10;y20;z30;昨天=星期一;今天是星期二;明天=结婚;printf(“%d %d %dn”,X,Y,Z);/ /输出:10 20 30printf(“%d %d %dn”,昨天,今天,明天);/ /输出:1 2 3方法四:类型定义,变量声明,赋初值同时进行。#包括枚举的一天11,星期二,结婚,清华大学,周五,坐,太阳昨天,妈妈,今天星期二明天结婚=,=;int10,y20,z30;无效main()printf(“%d %d %dn”,X,Y,Z);/ /输出:10 20 30print

18、f(“%d %d %dn”,昨天,今天,明天);/ /输出:1 2 33.2对枚举型的变量赋整数值时,需要进行类型转换。#包括枚举天 MON = 1,周二,周三,周四,周五,坐,太阳;无效main()昨天今天明天枚举;昨天,星期二;今天=(枚举日(昨天)+ 1);/类型转换明天=(枚举天)30;/ /类型转换/明天= 3;/错误printf(“%d %d %dn”,昨天,今天,明天);/ /输出:2 3 303.3使用枚举型变量#包括枚举贝尔=“A”,退格键=“B”,HTAB = t,返回=“r”,换行符= n,vtab =“V”,空间=;枚举布尔假= 0,match_flag真;无效main

19、()int索引= 0;国际count_of_letter = 0;国际count_of_space = 0;一个字符 =“我是Ely efod”;match_flag = false;为(索引!=“0”;索引+ +)如果(空格)!=索引 count_of_letter + +;其他的match_flag =(枚举布尔)1;count_of_space + +;printf(“%s %d次%c”,match_flag?”匹配”:“不匹配”,count_of_space,换行符);printf(“伯爵的信:% C% C”,count_of_letter,换行符,回车);输出:2次比赛字母数:10按

20、任意键继续4。枚举类型与sizeof运算符#包括枚举逃脱贝尔=“A”,退格键=“B”,HTAB = t,返回=“r”,换行符= n,vtab =“V”,空间=;枚举布尔假= 0,match_flag真;无效main()printf(“%d”,sizeof(ENUM逃脱);/ 4字节printf(“%d”,sizeof(逃);/ 4字节printf(“%d”,sizeof(枚举布尔);/ 4字节printf(“%d”,sizeof(布尔);/ 4字节printf(“%d”,sizeof(match_flag);/ 4字节printf(“%d”,sizeof(空间);/ 4字节printf(“%d

21、”,sizeof(换行);/ 4字节printf(“%d”,sizeof(假);/ 4字节printf(“%d”,sizeof(0);/ 4字节typedef struct weekday_st枚举周太阳= 123456789,周一,周二、周三、周四、周五,坐;每天早上月亮,enum ,下午; weekday_st;int main(int argc、argv char * )printf(“sizeof(weekday_st)= %dn”,sizeof(weekday_st);printf(“sizeof(周日)= %dn”,sizeof(weekday_st:周);printf(“size

22、of(天)= %dn”,sizeof(weekday_st:天);返回0;sizeof(weekday_st)= 1sizeof(周日)= 4sizeof(天)= 4printf(“sizeof(weekday_st)= %dn”,sizeof(weekday_st);这里因为结构体里面并没有定义任何的变量所以这个结构是空的,但是用sizeof进行运算的话也不能返回0因为毕竟存在着这么一个东西所以这时sizeof返回一个在机器里面所能表达的最小的存储单位的值一般的PC里面最小的存储单位的值是字符所以sizeof返回1printf(“sizeof(周日)= %dn”,sizeof(weekday_st:周);printf(“sizeof(天)= %dn”,sizeof(weekday_st:天);因为枚举量是当作整型常量来处理的所以上面这两个sizeof都是返回4这里的枚举周和枚举天只是内嵌”类型”,而不是成员变量,如果改成:typedef struct weekday_st枚举周太阳= 123456789,周一,周二、周三、周四、周五,坐电子战;每天早上月亮,enum ,了下午; weekday_st;sizeof(weekday_st)= 8sizeof(周日)= 4sizeof(天)= 4

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 社会民生


经营许可证编号:宁ICP备18001539号-1