tags:

views:

9579

answers:

3

I'm creating a set of enum values, but I need each enum value to be 64 bits wide. If I recall correctly, an enum is generally the same size as an int; but I thought I read somewhere that (at least in GCC) the compiler can make the enum any width they need to be to hold their values. So, is it possible to have an enum that is 64 bits wide?

A: 

Yes, that's exactly right. An enum is at least as big as needed to hold the values. (Actually the size needed to hold all the value OR'd together, but that's usually the same as the size of the largest)

James Curran
Sorry, this is just incorrect, you aren't guaranteed to be able to store values that can't be stored in an int.
Robert Gamble
+13  A: 

An enum is only guaranteed to be large enough to hold int values. The compiler is free to choose the actual type used based on the enumeration constants defined so it can choose a smaller type if it can represent the values you define. If you need enumeration constants that don't fit into an int you will need to use compiler-specific extensions to do so.

Robert Gamble
+6  A: 

Taken from the current C Standard (C99): http://www.open-std.org/JTC1/SC22/WG14/www/docs/n1256.pdf

6.7.2.2 Enumeration specifiers
[...]
Constraints
The expression that defines the value of an enumeration constant shall be an integer constant expression that has a value representable as an int.
[...]
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.

Not that compilers are any good at following the standard, but essentially: If your enum holds anything else than an int, you're in deep "unsupported behavior that may come back biting you in a year or two" territory.

Michael Stum
having only that, the following is valid i think: enum { LAST = INT_MAX, LAST1, LAST2 }; so LAST2 is not representable in int, but there wasn't an expression defining it.
Johannes Schaub - litb
In the actual PDF it defines that: "The identifiers in an enumerator list are declared as constants that have type int[...]". I've omitted that to make it not too verbose.
Michael Stum
ah, thanks. that makes sense
Johannes Schaub - litb