enum Named integer constants with enumerations
enum Named integer constants with enumerations
enum defines integer constants for constant expressions, incrementing from 0 or set values. Names share one namespace; weak typing; compatible types vary.
| Category | Struct / Union / Enum |
|---|---|
| Kind | Keyword / Statement |
| C standard | C89 / C90 |
📝 Syntax
enum Color { RED, GREEN = 5, BLUE };
▶ Example
Edit the C code below and click Run; the server compiles and executes it:
Output:
RED = 0, GREEN = 5, selected = 6
🏷️ Related struct/union/enum
📚 Related Tutorials
❓ FAQ
QIs the first enum value always 0?
AOnly an uninitialized first item defaults to 0. In enum E { A = 3, B };, A is 3 and B automatically becomes 4. Each enumeration constant can be assigned its own value.
QCan enumeration constants be negative or have duplicate values?
AYes, provided the values meet the applicable standard version's requirements for representation as int constants. For example, A = -1 and B = -1 are valid. The identifiers themselves cannot be duplicated in the same namespace.
QCan an enum variable hold only values listed in the enumeration?
AC does not impose that restriction. An enum variable can hold other representable integer values according to its compatible integer type. Compiler warnings can help, but the language does not make it a closed-set type.