首页 > 代码库 > 不明白的sizeof(enum)数据结构存储问题
不明白的sizeof(enum)数据结构存储问题
不明白的sizeof(enum)数据结构存储问题typedef struct weekday_st{ enum week {sun=123456789,mon,tue,wed,thu,fri,sat,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,aa,ab,ac,ad,ae,af,ag,ah,ai,aj,ak}; enum day{monring, moon, aftermoon};}weekday_st;int main(int argc, char *argv[]){ printf("sizeof(weekday_st)=%d\n", sizeof(weekday_st)); printf("sizeof(weekday)=%d\n", sizeof(weekday_st::week)); printf("sizeof(day)=%d\n", sizeof(weekday_st::day)); return 0;}sizeof(weekday_st)=1sizeof(weekday)=4sizeof(day)=4为什么sizeof整个结构是1,而无论enum里面的元素有几个,第一个初始值是多少,都是4呢?
对啊 emum本身就占4字节啊
你没有定义emum的变量
你只是声明了2个emum的数据类型而已
你在enum后面定义变量就变了,你试试
printf("sizeof(weekday_st)=%d\n", sizeof(weekday_st));
这里因为结构体里面并没有定义任何的变量
所以这个结构是空的,但是用sizeof进行运算的话也不能返回0
因为毕竟存在着这么一个东西
所以这时sizeof返回一个在机器里面所能表达的最小的存储单位的值
一般的PC里面最小的存储单位的值是char
所以sizeof返回1
printf("sizeof(weekday)=%d\n", sizeof(weekday_st::week));
printf("sizeof(day)=%d\n", sizeof(weekday_st::day));
因为枚举量是当作整型常量来处理的
所以上面这两个sizeof都是返回4
现在大部分编译器中,默认情况,enum会转化为int
但是每一个enum元素都是int类型,应该int * 元素个素才对,但无论enum里有几个值,其sizeof都是4,不解。
typedef struct weekday_st
{
enum week {sun=123456789,mon,tue,wed,thu,fri,sat,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,aa,ab,ac,ad,ae,af,ag,ah,ai,aj,ak};
enum day{monring, moon, aftermoon};
}weekday_st;
-----------------------------------------------------------------------------------
你这里的enum week和enum day只是内嵌“类型”,而不是成员变量,如果你改成:
typedef struct weekday_st
{
enum week {sun=123456789,mon,tue,wed,thu,fri,sat,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,aa,ab,ac,ad,ae,af,ag,ah,ai,aj,ak} ew;
enum day{monring, moon, aftermoon} ed;
}weekday_st;
现在你sizeof一下,即应该是8了。
楼上高见,的确是8了,但sizeof(week),不论week里的元素是多少个都是4,这又如何解析呢?
enum相当于是一个宏... 表示一个UINT数...
楼主,你基本知识缺太多了,需要补课。
enum只是定义了一个常量集合,里面没有“元素”。
不要和struct搞混了。
你enum week ed; ed.mon是编译不过的。
C++ Primer里说过: enum实参被提升的时候,提升到下一个能够表示其所有枚举常量的类型.: int,unsigned int,long ,unsigned long,所以在你的机子上,sizeof(enum)都等于4也是正常的
关于枚举体变量及其成员常量之大小(sizeof)
#include <stdio.h>int main(){ enum foo{}; enum gender{male, female}; enum season{spring, summer, autumn, winter}; enum week{sunday, monday, tuesday, wednesday, thursday, friday, saturday}; printf("%d\n",sizeof(enum));//为什么这也是合法的?结果为4 printf("%d\n",sizeof(foo));//结果为4 printf("%d\n",sizeof(gender));//结果为4 printf("%d\n",sizeof(season));//结果为4 printf("%d\n",sizeof(week));//结果为4 printf("%d\n",sizeof(male));//结果为4 return 0;
}
上面答代码是在Visual C++ 6.0下测试的。试问为什么结果都是4呢?
枚举类型举例详解
说明:
枚举型是预处理指令#define的替代。
枚举型是一个集合,集合中的元素(枚举成员)是一些命名的整型常量,元素之间用逗号,隔开。
类型定义以分号;结束。
第一个枚举成员的默认值为整型的0,后续枚举成员的值在前一个成员上加1。
联发科的一道笔试题:
- #include <iostream>
- #include <stdio.h>
- using namespace std;
- enum value
- {
- value1,
- value2,
- value3=0,
- value4=50,
- value5,
- };
- void main()
- {
- int i=1;
- do{
- cout<<value5+i<<",";
- i++;
- if(i<4)
- continue;
- break;
- }while(value2);
- }
枚举与sizeof
- #include <stdio.h>
- enum WEEK
- {
- sat=2,
- sun,
- mon=11,
- tue,
- };
- int main(void)
- {
- printf("%d %d %d\n",sun,tue,sizeof(tue)); //3 12 4
- printf("%d %d %d\n",sizeof(0),sizeof(WEEK),sizeof(sat)); //4 4 4
- return 0;
- }
- #include<stdio.h>
- enum
- {
- BELL = ‘\a‘,
- BACKSPACE = ‘\b‘,
- HTAB = ‘\t‘,
- RETURN = ‘\r‘,
- NEWLINE = ‘\n‘,
- VTAB = ‘\v‘,
- SPACE = ‘ ‘
- };
- enum BOOLEAN { FALSE = 0, TRUE } match_flag;
- void main()
- {
- int index = 0;
- int count_of_letter = 0;
- int count_of_space = 0;
- char str[] = "I‘m Ely efod";
- match_flag = FALSE;
- for(; str[index] != ‘\0‘; index++)
- if( SPACE != str[index] )
- count_of_letter++;
- else
- {
- match_flag = (enum BOOLEAN) 1;
- count_of_space++;
- }
- printf("%s %d times %c", match_flag ? "match" : "not match", count_of_space, NEWLINE);
- printf("count of letters: %d %c%c", count_of_letter, NEWLINE, RETURN);
- }
struct A
{
enum day{monring, moon, aftermoon};
};
sizeof(A) //1
sizeof(A::day) //4
求大牛指点
struct A
{
enum day{monring, moon, aftermoon} today;
};
改成以上内容后再求sizeof(A),就是4了
这是因为,enum day{monring, moon, aftermoon};只是定义了一个enum的枚举类型,而没有实际创建一个属于该类型的变量,所以struct A中其实是什么都没有的空结构体,空结构体的大小就是1个字节
个人理解:
sizeof(A) // 结构体里面的成员数量 确实是1个
sizeof(A::day) // day里面的数量
只是定义了一个枚举类型 day,没有定义成员变量。
enum day它没有空间,它只是定义了一些常量而已。在汇编中就相当于立即数。
所以,sizeof(A),看成一个空结构体,
在C语言中枚举的类型是int,这是标准规定的
sizeof(A::day)为4.
C语言详解 - 枚举类型注:以下全部代码的执行环境为VC++ 6.0在程序中,可能需要为某些整数定义一个别名,我们可以利用预处理指令#define来完成这项工作,您的代码可能是:#define MON 1#define TUE 2#define WED 3#define THU 4#define FRI 5#define SAT 6#define SUN 7在此,我们定义一种新的数据类型,希望它能完成同样的工作。这种新的数据类型叫枚举型。1. 定义一种新的数据类型 - 枚举型 以下代码定义了这种新的数据类型 - 枚举型enum DAY{ MON=1, TUE, WED, THU, FRI, SAT, SUN};(1) 枚举型是一个集合,集合中的元素(枚举成员)是一些命名的整型常量,元素之间用逗号,隔开。(2) DAY是一个标识符,可以看成这个集合的名字,是一个可选项,即是可有可无的项。(3) 第一个枚举成员的默认值为整型的0,后续枚举成员的值在前一个成员上加1。(4) 可以人为设定枚举成员的值,从而自定义某个范围内的整数。(5) 枚举型是预处理指令#define的替代。(6) 类型定义以分号;结束。2. 使用枚举类型对变量进行声明新的数据类型定义完成后,它就可以使用了。我们已经见过最基本的数据类型,如:整型int, 单精度浮点型float, 双精度浮点型double, 字符型char, 短整型short等等。用这些基本数据类型声明变量通常是这样:char a; //变量a的类型均为字符型charchar letter;int x, y, z; //变量x,y和z的类型均为整型intint number;double m, n;double result; //变量result的类型为双精度浮点型double既然枚举也是一种数据类型,那么它和基本数据类型一样也可以对变量进行声明。方法一:枚举类型的定义和变量的声明分开enum DAY{ MON=1, TUE, WED, THU, FRI, SAT, SUN};enum DAY yesterday;enum DAY today;enum DAY tomorrow; //变量tomorrow的类型为枚举型enum DAYenum DAY good_day, bad_day; //变量good_day和bad_day的类型均为枚举型enum DAY方法二:类型定义与变量声明同时进行:enum //跟第一个定义不同的是,此处的标号DAY省略,这是允许的。{ saturday, sunday = 0, monday, tuesday, wednesday, thursday, friday} workday; //变量workday的类型为枚举型enum DAYenum week { Mon=1, Tue, Wed, Thu, Fri Sat, Sun} days; //变量days的类型为枚举型enum weekenum BOOLEAN { false, true } end_flag, match_flag; //定义枚举类型并声明了两个枚举型变量方法三:用typedef关键字将枚举类型定义成别名,并利用该别名进行变量声明:typedef enum workday{ saturday, sunday = 0, monday, tuesday, wednesday, thursday, friday} workday; //此处的workday为枚举型enum workday的别名workday today, tomorrow; //变量today和tomorrow的类型为枚举型workday,也即enum workdayenum workday中的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. 使用枚举类型的变量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 \n", yesterday, today, tomorrow);}方法二:声明变量的同时赋初值#include <stdio.h>/* 定义枚举类型 */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 \n", 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 \n", x, y, z); //输出:10 20 30 printf("%d %d %d \n", 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 \n", x, y, z); //输出:10 20 30 printf("%d %d %d \n", yesterday, today, tomorrow); //输出:1 2 3}3.2 对枚举型的变量赋整数值时,需要进行类型转换。#include <stdio.h>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 \n", yesterday, today, tomorrow); //输出:2 3 30}3.3 使用枚举型变量#include<stdio.h>enum{ BELL = ‘\a‘, BACKSPACE = ‘\b‘, HTAB = ‘\t‘, RETURN = ‘\r‘, NEWLINE = ‘\n‘, VTAB = ‘\v‘, SPACE = ‘ ‘};enum BOOLEAN { FALSE = 0, TRUE } match_flag;void main(){ int index = 0; int count_of_letter = 0; int count_of_space = 0; char str[] = "I‘m Ely efod"; match_flag = FALSE; for(; str[index] != ‘\0‘; index++) if( SPACE != str[index] ) count_of_letter++; else { match_flag = (enum BOOLEAN) 1; count_of_space++; } printf("%s %d times %c", match_flag ? "match" : "not match", count_of_space, NEWLINE); printf("count of letters: %d %c%c", count_of_letter, NEWLINE, RETURN);}输出:match 2 timescount of letters: 10Press any key to continue4. 枚举类型与sizeof运算符#include <stdio.h>enum escapes{ BELL = ‘\a‘, BACKSPACE = ‘\b‘, HTAB = ‘\t‘, RETURN = ‘\r‘, NEWLINE = ‘\n‘, VTAB = ‘\v‘, SPACE = ‘ ‘};enum BOOLEAN { FALSE = 0, TRUE } match_flag;void main(){ printf("%d bytes \n", sizeof(enum escapes)); //4 bytes printf("%d bytes \n", sizeof(escapes)); //4 bytes printf("%d bytes \n", sizeof(enum BOOLEAN)); //4 bytes printf("%d bytes \n", sizeof(BOOLEAN)); //4 bytes printf("%d bytes \n", sizeof(match_flag)); //4 bytes printf("%d bytes \n", sizeof(SPACE)); //4 bytes printf("%d bytes \n", sizeof(NEWLINE)); //4 bytes printf("%d bytes \n", sizeof(FALSE)); //4 bytes printf("%d bytes \n", sizeof(0)); //4 bytes}5. 综合举例#include<stdio.h>enum Season{ spring, summer=100, fall=96, winter};typedef enum{ Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday}Weekday;void main(){ /* Season */ printf("%d \n", spring); // 0 printf("%d, %c \n", summer, summer); // 100, d printf("%d \n", fall+winter); // 193 Season mySeason=winter; if(winter==mySeason) printf("mySeason is winter \n"); // mySeason is winter int x=100; if(x==summer) printf("x is equal to summer\n"); // x is equal to summer printf("%d bytes\n", sizeof(spring)); // 4 bytes /* Weekday */ printf("sizeof Weekday is: %d \n", sizeof(Weekday)); //sizeof Weekday is: 4 Weekday today = Saturday; Weekday tomorrow; if(today == Monday) tomorrow = Tuesday; else tomorrow = (Weekday) (today + 1); //remember to convert from int to Weekday}
C语言详解 - 枚举类型注:以下全部代码的执行环境为VC++ 6.0在程序中,可能需要为某些整数定义一个别名,我们可以利用预处理指令#define来完成这项工作,您的代码可能是:复制代码#define MON 1#define TUE 2#define WED 3#define THU 4#define FRI 5#define SAT 6#define SUN 7复制代码 在此,我们定义一种新的数据类型,希望它能完成同样的工作。这种新的数据类型叫枚举型。 1. 定义一种新的数据类型 - 枚举型 以下代码定义了这种新的数据类型 - 枚举型enum DAY{ MON=1, TUE, WED, THU, FRI, SAT, SUN}; (1) 枚举型是一个集合,集合中的元素(枚举成员)是一些命名的整型常量,元素之间用逗号,隔开。(2) DAY是一个标识符,可以看成这个集合的名字,是一个可选项,即是可有可无的项。(3) 第一个枚举成员的默认值为整型的0,后续枚举成员的值在前一个成员上加1。(4) 可以人为设定枚举成员的值,从而自定义某个范围内的整数。(5) 枚举型是预处理指令#define的替代。(6) 类型定义以分号;结束。 2. 使用枚举类型对变量进行声明新的数据类型定义完成后,它就可以使用了。我们已经见过最基本的数据类型,如:整型int, 单精度浮点型float, 双精度浮点型double, 字符型char, 短整型short等等。用这些基本数据类型声明变量通常是这样:复制代码char a; //变量a的类型均为字符型charchar letter;int x, y, z; //变量x,y和z的类型均为整型intint number;double m, n;double result; //变量result的类型为双精度浮点型double复制代码 既然枚举也是一种数据类型,那么它和基本数据类型一样也可以对变量进行声明。方法一:枚举类型的定义和变量的声明分开enum DAY{ MON=1, TUE, WED, THU, FRI, SAT, SUN}; enum DAY yesterday;enum DAY today;enum DAY tomorrow; //变量tomorrow的类型为枚举型enum DAYenum DAY good_day, bad_day; //变量good_day和bad_day的类型均为枚举型enum DAY 方法二:类型定义与变量声明同时进行:复制代码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; //定义枚举类型并声明了两个枚举型变量 方法三:用typedef关键字将枚举类型定义成别名,并利用该别名进行变量声明:复制代码typedef enum workday{ saturday, sunday = 0, monday, tuesday, wednesday, thursday, friday} workday; //此处的workday为枚举型enum workday的别名复制代码 workday today, tomorrow; //变量today和tomorrow的类型为枚举型workday,也即enum workday enum workday中的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. 使用枚举类型的变量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 \n", yesterday, today, tomorrow);}复制代码 方法二:声明变量的同时赋初值复制代码#include <stdio.h>/* 定义枚举类型 */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 \n", 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 \n", x, y, z); //输出:10 20 30 printf("%d %d %d \n", 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 \n", x, y, z); //输出:10 20 30 printf("%d %d %d \n", yesterday, today, tomorrow); //输出:1 2 3}复制代码 3.2 对枚举型的变量赋整数值时,需要进行类型转换。复制代码#include <stdio.h>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 \n", yesterday, today, tomorrow); //输出:2 3 30}复制代码 3.3 使用枚举型变量复制代码#include<stdio.h>enum{ BELL = ‘\a‘, BACKSPACE = ‘\b‘, HTAB = ‘\t‘, RETURN = ‘\r‘, NEWLINE = ‘\n‘, VTAB = ‘\v‘, SPACE = ‘ ‘};enum BOOLEAN { FALSE = 0, TRUE } match_flag;void main(){ int index = 0; int count_of_letter = 0; int count_of_space = 0; char str[] = "I‘m Ely efod"; match_flag = FALSE; for(; str[index] != ‘\0‘; index++) if( SPACE != str[index] ) count_of_letter++; else { match_flag = (enum BOOLEAN) 1; count_of_space++; } printf("%s %d times %c", match_flag ? "match" : "not match", count_of_space, NEWLINE); printf("count of letters: %d %c%c", count_of_letter, NEWLINE, RETURN);}复制代码 输出:match 2 timescount of letters: 10Press any key to continue 4. 枚举类型与sizeof运算符复制代码#include <stdio.h>enum escapes{ BELL = ‘\a‘, BACKSPACE = ‘\b‘, HTAB = ‘\t‘, RETURN = ‘\r‘, NEWLINE = ‘\n‘, VTAB = ‘\v‘, SPACE = ‘ ‘};enum BOOLEAN { FALSE = 0, TRUE } match_flag;void main(){ printf("%d bytes \n", sizeof(enum escapes)); //4 bytes printf("%d bytes \n", sizeof(escapes)); //4 bytes printf("%d bytes \n", sizeof(enum BOOLEAN)); //4 bytes printf("%d bytes \n", sizeof(BOOLEAN)); //4 bytes printf("%d bytes \n", sizeof(match_flag)); //4 bytes printf("%d bytes \n", sizeof(SPACE)); //4 bytes printf("%d bytes \n", sizeof(NEWLINE)); //4 bytes printf("%d bytes \n", sizeof(FALSE)); //4 bytes printf("%d bytes \n", sizeof(0)); //4 bytes}复制代码 5. 综合举例复制代码#include<stdio.h>enum Season{ spring, summer=100, fall=96, winter};typedef enum{ Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday}Weekday;void main(){ /* Season */ printf("%d \n", spring); // 0 printf("%d, %c \n", summer, summer); // 100, d printf("%d \n", fall+winter); // 193 Season mySeason=winter; if(winter==mySeason) printf("mySeason is winter \n"); // mySeason is winter int x=100; if(x==summer) printf("x is equal to summer\n"); // x is equal to summer printf("%d bytes\n", sizeof(spring)); // 4 bytes /* Weekday */ printf("sizeof Weekday is: %d \n", sizeof(Weekday)); //sizeof Weekday is: 4 Weekday today = Saturday; Weekday tomorrow; if(today == Monday) tomorrow = Tuesday; else tomorrow = (Weekday) (today + 1); //remember to convert from int to Weekday}复制代码
不明白的sizeof(enum)数据结构存储问题
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。