Suppose I have (on a 32 bit machine)
enum foo { val1 = 0x7FFFFFFF, // originally '2^31 - 1' val2, val3 = 0xFFFFFFFF, // originally '2^32 - 1' val4, val5 };
what is the value of val2, val4 and val5? I know I could test it, but is the result standardized?
On an 8-bit processor, enums can be 16-bits wide. On a 32-bit processor they can be 32-bits wide or more or less. The GCC C compiler will allocate enough memory for an enum to hold any of the values that you have declared. So, if your code only uses values below 256, your enum should be 8 bits wide.
An ENUM column can have a maximum of 65,535 distinct elements. If you retrieve an ENUM value in a numeric context, the column value's index is returned.
Answer. An enum is effectively a signed integer with a potential value range from -32768 to +32765. However, an enum can be stored in either one or two bytes, depending on what values are assigned to it.
In C standard:
C11 (n1570), § 6.7.2.2 Enumeration specifiers
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.
If the underlying type used by the compiler is not capable to represent these values, the behavior is undefined.
C11 (n1570), § 4. Conformance
If a ‘‘shall’’ or ‘‘shall not’’ requirement that appears outside of a constraint or runtime-constraint is violated, the behavior is undefined.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With