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?
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.
enum
should be larger than an int
or smaller? Following @MichaelStum 's answer your first sentence should be "An enum
is only guaranteed to fit into an int
value." –
Huehuebner int
. Michael Stum's answer, which references C99, says that an enum may be as small as a char
. –
Bedim enum
is only guaranteed to be large enough to hold the value of the largest enumerator in the enum. –
Musing int
values. It is a correct statement. Your statement is actually incorrect. An enum
will not hold the largest enumerator if that enumerator is larger than an int
. I learned this by having my 64-bit enumerator truncated to the maximum of int
(which is 32-bit in my case). –
Pseudocarp int
. The behaviour you described is compiler-specific extensions –
Musing 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.
Update: The latest publicly available draft of the C Standard (C11): http://www.open-std.org/JTC1/SC22/WG14/www/docs/n1570.pdf contains the same clauses. Hence, this answer still holds for C11.
int
. short
and long
are integer types too, and whatever the implementation picks, all values must fit ("shall be capable of representing the values of all the members of the enumeration"). –
Primulaceous int
, the actual enumeration variable could be another type. This is a well-known inconsistency in the standard. –
Fiscal enum my_enum { my_value }
, my_value
will have type int
, but enum my_enum
can have an implementation defined type which must at least represent all the enumeration values. So my_value
may have a narrowing conversion to enum my_enum
, but it's guaranteed not to overflow. –
Breast 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.
enum
should be larger than an int
or smaller? Following @MichaelStum 's answer your first sentence should be "An enum
is only guaranteed to fit into an int
value." –
Huehuebner int
. Michael Stum's answer, which references C99, says that an enum may be as small as a char
. –
Bedim enum
is only guaranteed to be large enough to hold the value of the largest enumerator in the enum. –
Musing int
values. It is a correct statement. Your statement is actually incorrect. An enum
will not hold the largest enumerator if that enumerator is larger than an int
. I learned this by having my 64-bit enumerator truncated to the maximum of int
(which is 32-bit in my case). –
Pseudocarp int
. The behaviour you described is compiler-specific extensions –
Musing While the previous answers are correct, some compilers have options to break the standard and use the smallest type that will contain all values.
Example with GCC (documentation in the GCC Manual):
enum ord {
FIRST = 1,
SECOND,
THIRD
} __attribute__ ((__packed__));
STATIC_ASSERT( sizeof(enum ord) == 1 )
Just set the last value of the enum to a value large enough to make it the size you would like the enum to be, it should then be that size:
enum value{a=0,b,c,d,e,f,g,h,i,j,l,m,n,last=0xFFFFFFFFFFFFFFFF};
sizeof(a)
is 4 and sizeof(last)
is 8. –
Supplementary We have no control over the size of an enum
variable. It totally depends on the implementation, and the compiler gives the option to store a name for an integer using enum
, so enum
is following the size of an integer.
In C language, an enum
is guaranteed to be of size of an int
. There is a compile time option (-fshort-enums
) to make it as short (This is mainly useful in case the values are not more than 64K). There is no compile time option to increase its size to 64 bit.
Consider this code:
enum value{a,b,c,d,e,f,g,h,i,j,l,m,n};
value s;
cout << sizeof(s) << endl;
It will give 4 as output. So no matter the number of elements an enum
contains, its size is always fixed.
© 2022 - 2024 — McMap. All rights reserved.
enum
is in fact memory use. Is memory optimization dead or something, or does everyone think the compiler is the center of the universe still and it automagically makes everything fast and optimal without any effort on the part of the programmer? It's absurd to use a larger data type than you need, and if I only need 256 values or less for my enum, then why do I need 16 or 32-bit words to store them? (Data model isn't an excuse. The values usually are quite easily sign-extended such as when stored in the registers.) – Univalence1<<31
and1<<30
with huge gaps in between them. – Archerfish