Clang编译器的C枚举的数据类型是什么?
我发布了其他问题: 我应该使用什么类型C 枚举的二进制表示?,根据答案,我必须知道编译器的枚举数据类型。
Clang 编译器上的 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.
What's the data-type of C enum on Clang compiler?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
与大多数(可能是所有)C 编译器一样,枚举类型的大小可能会有所不同。这是一个示例程序及其输出:
输出:
该标准所需的全部内容是:
快速的网络搜索并没有找到指定其行为的 clang 手册,但几乎可以肯定在某个地方有一份手册。
Like most (all, maybe) C compilers, the size of an enumerated type can vary. Here's an example program and its output:
Output:
All that the standard requires is:
A quick web search didn't turn up a clang manual that specified its behaviour, but one is almost certainly out there somewhere.
除了卡尔的答案之外,如果有任何更适合的话,它甚至可能与有符号或无符号类型兼容。例如,它可以,但不能在
有符号字符
中有small
,在无符号字符中有
。big
字符顺便说一句,常量
m
、a
和b
不是枚举类型,但始终是int
类型。编辑:我刚刚使用大于
int
的值进行了测试。在 C99 模式下,gcc
发出错误(这是正确的),但clang
仅发出警告,并且具有更宽的类型来表示enum
。In addition to Carl's answer it might even be compatible to a signed or unsigned type if anything of that fits better. E.g it could but mustn't in
have
small
in asigned char
andbig
in anunsigned char
.BTW the constants
m
,a
andb
are not of enumeration type but always of typeint
.Edit: I just tested with values larger than
int
. In C99 modegcc
emits an error (which is correct) butclang
only issues a warning and than has a wider type to represent theenum
.好吧,编译器可能会选择足够大的整数大小,但我猜它会选择“本机”大小(“字”=寄存器大小,对于 x86 32 位模式应该是 long ,对于 x64 应该是 long long )。对于您的私有结构,您不应该关心,但是如果您想将其序列化到文件或通过网络,那么您应该显式使用足够大(例如长)的整数类型,以便您可以使用另一种编译器/语言来选择它没有头痛。
如果你真的想知道,只需询问编译器:
Well, a compiler may chose an integer size big enough but I guess it will chose a "native" size (a "word" = size of register, should be long for x86 32bit mode or long long for x64). For your private struct you shouldn't care, but if you want to serialize it to a file or over the network then you should explicitly use an integer type that's big enough (e.g. long) so you can pick it up with another compiler/language without headaches.
If you really wanna know, just ask the compiler: