is the size of an enum always the same among different compilers (gcc, visual c and others?). That is, does sizeof() of a specific enum gives the same values with every compiler that follow the C/C++ standards?
-
And the signedness may differ too. – Matthieu M. Sep 07 '11 at 17:15
2 Answers
No.
In both C and C++ an enum will have a size such that all the values can be represented and be compatible with an integer type. Different compilers may use different algorithm to choose the type (if it is not specified by another standard such a clearly defined ABI). (C++11 allows to specify the underlying type with a new syntax)

- 51,233
- 8
- 91
- 143
-
Not necessarily: C allows the type of enumeration values to be chosen by the compiler, just as, according to your answer, does C++. See section 6.7.2.2/4 in [the C Standard](http://www.open-std.org/JTC1/sc22/wg14/www/docs/n1256.pdf). – pmg Sep 07 '11 at 14:04
-
Additionally, in C++0x you can use the new *enum class* to specify the integer type that will be used to back the enumeration values. – David Rodríguez - dribeas Sep 07 '11 at 14:06
-
@pmg, thanks. I don't know where I got that. I checked it was already the case in C90. – AProgrammer Sep 07 '11 at 14:12
"Each enumerated type shall be compatible with char, a signed integer type, or an unsigned integer type. The choice of type is implementation-defined) but shall be capable of representing the values of all the members of the enumeration."
"...An implementation may delay the choice of which integer type until all enumeration constants have been seen."
ISO/IEC 9899:1999 (E) p.105
So we only have upper boundaries for sizeof(enum). On most systems i had sizeof(enum) = 4, but STM compiler made sizeof(enum) = 1/2/4 depending on values written in enum
Edit: it seems that you can set one of your enum's value to max int to ensure that compiler chooses integer as enum size.

- 2,287
- 3
- 19
- 29