- An enum is a "named collection of constants":
enum MyType_e {A, B, C};
- Those constants are declared in the parent scope of the enum i.e. if the enum is declared in file scope, and is unnamed, it is equivalent to a series of e.g.
#define A 0
statements - The underlying type for enum constants is always
int
i.e.int var = A
is fully legal, althoughvar
is not of typeMyType_e
So what purpose does the enum name serve?
EDIT As per the comments below, my understanding of enums appears to be quite flawed. An enum has nothing to do with #define statements. Enums are resolved at compile time, and are typed.
int
...". This is kind of confusing, though, since it later goes on to say, "Each enumerated type shall be compatible withchar
, a signed integer type, or an unsigned integer type. The choice of type is implementation-defined...." So the constants themselves areint
, but theenum
type itself might not be. By "namespace", I think Vorac meant "scope". – jamesdlin#define
statements. The question has lots of upvotes, but it contains fundamentally wrong assumptions. – Paul Hankin