Clang编译器的C枚举的数据类型是什么? [英] What's the data-type of C enum of Clang compiler?
问题描述
我发布了其他问题:什么类型我应该使用C枚举的二进制表示吗?,通过答案,我必须知道我的编译器的枚举数据类型。
I posted other question: What type should I use for binary representation of C enum?, and by the answer, I have to know my compiler's enum data-type.
类型的C枚举Clang编译器?
What's the data-type of C enum on Clang compiler?
推荐答案
像大多数(所有,也许)C编译器,枚举类型的大小变化。下面是一个示例程序及其输出:
Like most (all, maybe) C compilers, the size of an enumerated type can vary. Here's an example program and its output:
#include <stdio.h>
typedef enum
{
val1 = 0x12
} type1;
typedef enum
{
val2 = 0x123456789
} type2;
int main(int argc, char **argv)
{
printf("1: %zu\n2: %zu\n", sizeof(type1), sizeof(type2));
return 0;
}
输出:
1: 4
2: 8
该标准要求是:
All that the standard requires is:
类型的选择是实现定义的,但应能够表示所有成员的值枚举。
The choice of type is implementation-defined, but shall be capable of representing the values of all the members of the enumeration.
一个快速的网页搜索没有打开一个clang手册指定它的行为,但一个几乎肯定在那里
A quick web search didn't turn up a clang manual that specified its behaviour, but one is almost certainly out there somewhere.
这篇关于Clang编译器的C枚举的数据类型是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!