Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
417 views
in Technique[技术] by (71.8m points)

Signedness of enum in C/C99/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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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:

enum SignedEnum { a = -1 };

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

enum ShortEnum : short { a };

(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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...