4 ms·
Yes, but only for constants of type int. For example, this is valid: enum { ANSWER = 42 }; But C enumeration constants are always of type int, so you can
by _kst_ 7y ago
Yes, but only for constants of type int.
For example, this is valid:
enum { ANSWER = 42 };
But C enumeration constants are always of type int, so you can't define a constant of some other integer type this way.
- loeg 7y agoIt's worse than that. The type of a C enumeration is implementation defined. It must cover the full range of defined values, but it definitely doesn't have to be 'int'. (§ 6.7.2.2 (4)): > Each enumerated type shall be compatible with [ed: one of] 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. One possible source of confusion is that explicit values in an enum (i.e., '42' in your example) must have values representable as int. (§ 6.7.2.2, (2)).
- _kst_ 7y agoI said enumeration constants are of type int. For example, given: enum foo { this, that, the_other }; enum foo obj; obj is of type "enum foo", which is compatible with some implementation-defined integer type, but the constants "this", "that", and "the_other" are of type int. In C++, the constants are of type "enum foo", which can also be referred to as "foo".