在C / C99 / C ++ / C ++ X / GNU C / GNU C99枚举的符号性 [英] Signedness of enum in C/C99/C++/C++x/GNU C/GNU C99

查看:202
本文介绍了在C / C99 / C ++ / C ++ X / GNU C / GNU C99枚举的符号性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

枚举类型符号或无符号?是枚举的符号性用C不同/ C99 / ANSI C / C ++ / C ++ X / GNU C / GNU C99?

Is enum type signed or unsigned? Is the Signedness of enums differ in C/C99/ANSI C/C++/C++x/GNU C/ GNU C99?

感谢

推荐答案

这是枚举是保证再一个整数psented $ P $,但实际类型(以及它的符号性)是实现相关的。

An enum is guaranteed to be represented by an integer, but the actual type (and its signedness) is implementation-dependent.

您可以通过给统计员的一个负值强制枚举是由符号类型psented重新$ P $:

You can force an enumeration to be represented by a signed type by giving one of the enumerators a negative value:

enum SignedEnum { a = -1 };

在C ++ 0x中,枚举的基础类型可以显式指定:

In C++0x, the underlying type of an enumeration can be explicitly specified:

enum ShortEnum : short { a };

(C ++ 0x中还增加了对枚举范围内的支持)

(C++0x also adds support for scoped enumerations)

有关完整,我会补充一点的 C程序设计语言,第二版的,枚举器将被指定为具有类型 INT (P 215页)。 K&安培; R是不是C标准,所以这不是规范的ISO C编译器,但它确实preDATE ISO C标准,因此它从历史的角度看是满有意思

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.

这篇关于在C / C99 / C ++ / C ++ X / GNU C / GNU C99枚举的符号性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆