SPNU151W January 1998 – March 2023 66AK2E05 , 66AK2H06 , 66AK2H12 , 66AK2H14 , AM1705 , AM1707 , AM1802 , AM1806 , AM1808 , AM1810 , AM5K2E04 , OMAP-L132 , OMAP-L137 , OMAP-L138 , SM470R1B1M-HT , TMS470R1A288 , TMS470R1A384 , TMS470R1A64 , TMS470R1B1M , TMS470R1B512 , TMS470R1B768
In the following declaration,
enum e
is an enumerated type. Each of a
and b
are enumeration
constants.
enum e { a, b=N };
Each enumerated type is assigned an integer type that can hold all of the enumeration constants. This integer type is the "underlying type." The type of each enumeration constant is also an integer type, and in C might not be the same type. Be careful to note the difference between the underlying type of an enumerated type and the type of an enumeration constant.
The size and signedness chosen for the enumerated type and each enumeration constant depend on the values of the enumeration constants and whether you are compiling for C or C++. C++11 allows you to specify a specific type for an enumeration type; if such a type is provided, it will be used and the rest of this section does not apply.
In C++ mode, the compiler allows enumeration constants up to the largest integral type (64 bits). The C standard says that all enumeration constants in strictly conforming C code (C89/C99/C11) must have a value that fits into the type "int;" however, as an extension, you may use enumeration constants larger than "int" even in C mode.
You may control the strategy for picking enumerated types by using either the --enum_type command line option, or by using an attribute, or both. If you use the --enum_type=packed option (the default), the compiler uses the smallest type it can for the enumerated type. If you use the --enum_type=int option, the underlying type will be int. An enumeration constant with a value outside the int range generates an error.
For the enumerated type if --enum_type=packed, the compiler selects the first type in this list that is big enough and of the correct sign to represent all of the values of the enumeration constants:
The "long" type is skipped because it is the same size as "int."
For example, this enumerated type will have "unsigned char" as its underlying type:
enum uc { a, b, c };
But this one will have "signed char" as its underlying type:
enum sc { a, b, c, d = -1 };
And this one will have "signed short" as its underlying type:
enum ss { a, b, c, d = -1, e = UCHAR_MAX };
For C++, the enumeration constants are all of the same type as the enumerated type.
For C, the enumeration constants are assigned types depending on their value. All enumeration constants with values that can fit into "int" are given type "int," even if the underlying type of the enumerated type is smaller than "int." All enumeration constants that do not fit in an "int" are given the same type as the underlying type of the enumerated type. This means that some enumeration constants may have a different size and signedness than the enumeration type.