enum Some_Flag {
SOME_FLAG_A = 0x00000000u,
SOME_FLAG_B = 0x00000001u,
SOME_FLAG_C = 0x00000002u,
/* ... */
SOME_FLAG_Z = 0x80000000u,
};
uint32_t a;
a = SOME_FLAG_Z;
Assuming 32 bit integers... Is this valid in C?
The standard seems ambiguous to me.
EDIT:
Quoting the standard:
6.4.4.3 Enumeration constants
Semantics
2 An identifier declared as an enumeration constant has type int. Forward references: enumeration specifiers (6.7.2.2).
6.7.2.2 Enumeration specifiers
Constraints
2 The expression that defines the value of an enumeration constant shall be an integer constant expression that has a value representable as an int.
Semantics
3 The identifiers in an enumerator list are declared as constants that have type int and may appear wherever such are permitted.127) An enumerator with = defines its enumeration constant as the value of the constant expression. If the first enumerator has no =, the value of its enumeration constant is 0. Each subsequent enumerator with no = defines its enumeration constant as the value of the constant expression obtained by adding 1 to the value of the previous enumeration constant. (The use of enumerators with = may produce enumeration constants with values that duplicate other values in the same enumeration.) The enumerators of an enumeration are also known as its members.
4 Each enumerated type shall be compatible with char, a signed integer type, or an unsigned integer type. The choice of type is implementation-defined,128) but shall be capable of representing the values of all the members of the enumeration. The enumerated type is incomplete until immediately after the } that terminates the list of enumerator declarations, and complete thereafter.
The constraints seem to clearly indicate that an enum is an int, but then 6.7.2.2_4 seems to allow unsigned ints ¿?
int
then0x8000u
is not even close to the limit. Do you mean0x80000000u
? – Vivacious