Nearby lessons
108 of 124C - enum
An enumeration gives names to a set of related integer constants. Learn the enum syntax, default and explicit values, how it beats a pile of #defines, and why an enum in C is not the airtight type it is in other languages.
Declaring an Enumeration
List the names inside braces. By default the first is 0 and each one after it is one more:
Explicit Values
Set any enumerator you like. Names that follow an explicit value continue counting from it:
Duplicate Values Are Legal
Two names may share a value. That is sometimes deliberate — an alias — but it also means you cannot map a value back to a single name:
Why Not Just #define?
#defines is a loose pile of numbers. An enum tells the compiler these values belong together, so it can auto-number them, keep them scoped, show the names in a debugger, and warn about a switch that forgets one.enum | #define | |
|---|---|---|
| Auto-numbering | Yes | You maintain it by hand |
| Grouped as one type | Yes | No |
| Visible in a debugger | Yes | No — already replaced |
| Respects scope | Yes | No |
switch completeness warning | Possible | Never |
enum With switch
This is the pairing enum exists for. Every case reads as English, and with -Wall the compiler warns if you omit one:
Printing the Name, Not the Number
C has no built-in way to print an enumerator's name. The usual fix is a parallel array of strings — keep it in the same order as the enum:
A Sentinel Count Is a Useful Habit
Adding a final ..._COUNT enumerator gives you the number of items automatically. Insert a new value anywhere above it and every loop and array size follows:
C Does Not Enforce the Range
enum variable in C is an integer with a friendly name, not a restricted set. Assigning 99 to an enum Colour compiles without complaint, so any function that indexes an array with an enum value must still validate it. This is the single biggest difference from enums in stricter languages.Bit Flags
Give each enumerator a distinct power of two and you can combine them with | and test them with &:
typedef, Anonymous Enums, and Scope
A typedef drops the enum keyword. An anonymous enum is a neat way to declare a group of constants with no variable type at all:
Common Mistakes
- Reusing an enumerator name — enumerators share the enclosing scope, so two enums cannot both define
OK. - Expecting the first value to be 1 — it is
0unless you say otherwise. - Trusting the range — validate before using an enum as an array index.
- Expecting to print the name —
printf("%d", RED)prints0; you need a name table. - Assuming a fixed size — the underlying type is implementation-defined.
- Forgetting the semicolon after
}— same rule as a structure. - Non-power-of-two values used as flags — the bits overlap and the tests give wrong answers.
enum waiting to be written. It costs three lines and removes a whole class of "what was 2 again?" bugs.- enum Colour { RED, GREEN, BLUE }; — RED is 0, GREEN 1, BLUE 2.
- You may set values explicitly; unset names continue from the last one.
- Enumerators are plain int constants known at compile time.
- enum pairs naturally with switch and gives the compiler a chance to warn.
- C does not stop you assigning any int to an enum variable.