在C语言中的sizeof枚举 [英] sizeof enum in C language
问题描述
我怎么能知道枚举天
的大小?这将是等于 7 * 4(的sizeof(int)的)= 28
??结果
在的printf()
这里给我值 4
,怎样才可以解释呢?
How can i know the size of the enum Days
? Will it be equal to 7*4(sizeof(int)) = 28
??
The printf()
here is giving me value 4
, How can it be explained?
enum Days
{
saturday,
sunday ,
monday ,
tuesday,
wednesday,
thursday,
friday
} TheDay;
printf("%d", sizeof(enum Days));
此外,我们可以使用它作为(枚举天)(0),这类似于array.If大小的整数等于4那么怎么这阵这种行为可以解释的?
Also we can use this as (enum Days)(0), which is similar to the integer array.If size is equal to 4 then how this array kind of behavior can be explained ?
推荐答案
在C中的所有枚举最键入 INT
的时间整数,这可以解释为什么的sizeof(天)== 4
为您服务。
In C all enums are most of the time integers of type int
, which explains why sizeof(Days) == 4
for you.
要知道有多少值是一个枚举
你可以做这样的事情:
To know how many values are in an enum
you can do something like this:
enum Days
{
saturday,
sunday,
monday,
tuesday,
wednesday,
thursday,
friday,
NUM_DAYS
};
然后 NUM_DAYS
将在天枚举数
。
请注意,如果你改变一个枚举的值,例如这是不行的:
Note that this will not work if you change the values of an enumeration, for example:
enum Foo
{
bar = 5,
NUM_FOO
};
在上面的枚举, NUM_FOO
将 6
。
这篇关于在C语言中的sizeof枚举的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!