C/C99/C++/C++x/GNU C/GNU C99 中枚举的符号
enum
类型是有符号的还是无符号的? C/C99/ANSI C/C++/C++x/GNU C/ GNU C99 之间的枚举符号是否不同?
谢谢
Is the enum
type signed or unsigned? Does the signedness of enums differ between: C/C99/ANSI C/C++/C++x/GNU C/ GNU C99?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
枚举保证由整数表示,但实际类型(及其符号)取决于实现。
您可以通过给枚举数之一赋予负值来强制枚举由有符号类型表示:
在 C++0x 中,可以显式指定枚举的基础类型:
(C++0x 还添加了对作用域枚举的支持)
为了完整起见,我将在《C 编程语言,第二版》中添加这一点。,枚举数被指定为具有类型
int
(第 215 页)。 K&R 不是 C 标准,因此对于 ISO C 编译器来说不是规范,但它确实早于 ISO C 标准,因此至少从历史的角度来看它很有趣。An enum is guaranteed to be represented by an integer, but the actual type (and its signedness) is implementation-dependent.
You can force an enumeration to be represented by a signed type by giving one of the enumerators a negative value:
In C++0x, the underlying type of an enumeration can be explicitly specified:
(C++0x also adds support for scoped enumerations)
For completeness, I'll add that in The C Programming Language, 2nd ed., enumerators are specified as having type
int
(p. 215). K&R is not the C standard, so that's not normative for ISO C compilers, but it does predate the ISO C standard, so it's at least interesting from a historical standpoint.这是一个老问题...但我刚刚发现:
您可以将它用作 2 位无符号索引,例如:
在 GCC ARM 中尝试过 - 不起作用。
另外,WinDbg 将 STRUCTNAME.i 显示为数字,而不是 v0-v3。
This is an old question... but I've just found out this:
You can use it as an 2-bit unsigned index, for example:
Tried it in GCC ARM - doesn't work.
Also, WinDbg shows STRUCTNAME.i as a number, not as v0-v3.