C Enumerations
In this page:
enum EnumName { VALUE_ONE, VALUE_TWO, VALUE_THREE };
enum EnumName variable_name = VALUE_ONE;
What is an Enumeration?
Writing status instead of magic numbers like 0, 1, 2 makes code far easier to read and maintain -- comparing if (status == ACTIVE) is immediately clear, unlike if (status == 1) which requires you to remember what 1 means.
Example: What is an Enumeration?
// Include standard input/output (printf, scanf)
#include <stdio.h>
enum Status { ACTIVE, INACTIVE };
// Program execution starts in main()
int main() {
enum Status status = ACTIVE;
// Check whether status == ACTIVE
if (status == ACTIVE) {
// Print formatted text to the screen
printf("Active");
}
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Defining an Enum
This declares a new type alongside a set of named integer constants belonging to it, similar in spirit to how struct introduces a new composite type -- both use the same curly-brace-and-semicolon syntax.
Example: Defining an Enum
// Include standard input/output (printf, scanf)
#include <stdio.h>
enum Color { RED, GREEN, BLUE };
// Program execution starts in main()
int main() {
enum Color c = GREEN;
// Print formatted text to the screen
printf("%d", c);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Enum Constant Values
enum Color {RED, GREEN, BLUE} assigns RED=0, GREEN=1, BLUE=2 automatically -- this predictable numbering is why enums work seamlessly as array indices or switch-case values.
Example: Enum Constant Values
// Include standard input/output (printf, scanf)
#include <stdio.h>
enum Color { RED, GREEN, BLUE };
// Program execution starts in main()
int main() {
// Print formatted text to the screen
printf("%d %d %d", RED, GREEN, BLUE);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Custom Enum Values
enum Status {ACTIVE=1, PENDING, CLOSED} explicitly sets ACTIVE to 1, and since PENDING and CLOSED are unassigned, they automatically continue the sequence as 2 and 3.
Example: Custom Enum Values
// Include standard input/output (printf, scanf)
#include <stdio.h>
enum Status { ACTIVE = 1, PENDING, CLOSED };
// Program execution starts in main()
int main() {
// Print formatted text to the screen
printf("%d %d %d", ACTIVE, PENDING, CLOSED);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Using Enums in switch Statements
Because switch requires constant integer expressions for its case labels, and enum constants are exactly that, they're one of the few types (besides plain integers and chars) that switch statements can branch on directly.
Example: Using Enums in switch Statements
// Include standard input/output (printf, scanf)
#include <stdio.h>
enum Color { RED, GREEN, BLUE };
// Program execution starts in main()
int main() {
enum Color c = GREEN;
// Choose a branch based on the value of c
switch (c) {
case RED: printf("Red"); break;
case GREEN: printf("Green"); break;
case BLUE: printf("Blue"); break;
}
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
- Assuming the compiler stops you from assigning an invalid integer to an enum variable, when C treats enums like ints.
- Forgetting that enumerators start at 0 by default unless you assign values.
- Printing an enum with
%sand expecting its name, when it holds an integer and needs%d.
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: