1. Introduction
An enumeration (enum) in C is a user-defined type consisting of a set of named integer constants, called enumerators. Enumerations improve code readability by replacing 'magic numbers' with descriptive names, such as representing days of the week or states in a state machine.
Cricket analogy: Naming match results WON, LOST, TIED, NO_RESULT instead of using raw codes 0,1,2,3 is exactly what an enum does, it replaces magic numbers with readable labels for scorecards.
Internally, each enumerator is just an int with a fixed value, so enums provide readability and organization benefits without changing runtime performance compared to plain integer constants.
Cricket analogy: Calling a fielding position SLIP is just as fast to process as calling it position code 3, like an enum, it's purely a labeling convenience with zero performance cost over a raw integer.
2. Syntax
enum EnumName {
CONST1,
CONST2,
CONST3
};
enum EnumName variableName;3. Explanation
By default, the first enumerator in an enum is assigned the integer value 0, and each subsequent enumerator automatically gets the value of the previous one plus 1, unless explicitly overridden. So in 'enum Color { RED, GREEN, BLUE };', RED = 0, GREEN = 1, and BLUE = 2.
Cricket analogy: In a three-format ranking enum, TEST=0, ODI=1, T20=2 auto-increments just like listing formats in order without manually numbering each one.
You can explicitly assign integer values to any enumerator, and subsequent unassigned enumerators continue incrementing from that new value. For example, 'enum Status { OK = 200, NOT_FOUND = 404, SERVER_ERROR = 500 };' assigns specific values matching real HTTP status codes. Enum values are not required to be unique — two enumerators can share the same underlying integer value. A variable of an enum type is stored using an integer representation (commonly the size of int), and can be used in comparisons and switch statements just like a regular integer, though C (unlike C++) does not perform strict type checking between an enum and a plain int.
Cricket analogy: Assigning enum Milestone {FIFTY=50, CENTURY=100, DOUBLE_CENTURY=200} maps directly to real run milestones, just as explicit enum values can match meaningful real-world numbers like HTTP codes.
Common mistake: assuming enumerator values always start at 0 even when a later enumerator has an explicit value. Remember the rule precisely: only enumerators WITHOUT an explicit assignment auto-increment from the PREVIOUS enumerator's value (default starting point is 0 only for the very first one if unassigned).
4. Example
#include <stdio.h>
enum Weekday { SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY };
enum HttpStatus { OK = 200, NOT_FOUND = 404, SERVER_ERROR = 500 };
int main(void) {
enum Weekday today = WEDNESDAY;
enum HttpStatus response = NOT_FOUND;
printf("today = %d\n", today);
printf("SATURDAY = %d\n", SATURDAY);
printf("response code = %d\n", response);
if (today == WEDNESDAY) {
printf("Midweek day.\n");
}
return 0;
}5. Output
today = 3
SATURDAY = 6
response code = 404
Midweek day.6. Key Takeaways
- enum defines a set of named integer constants (enumerators) grouped under one type.
- By default, the first unassigned enumerator is 0, and each next one is the previous value + 1.
- Explicit values can be assigned to any enumerator; subsequent ones continue incrementing from there.
- Enum values are not required to be unique — duplicates are legal.
- Enumerators behave like plain int constants and work seamlessly in switch statements and comparisons.
- Enums improve readability compared to using raw magic numbers or #define constants for related groups of values.
Practice what you learned
1. In 'enum Color { RED, GREEN, BLUE };', what is the default value of GREEN?
2. Given 'enum Status { A, B = 5, C, D };', what is the value of D?
3. Are duplicate values allowed among different enumerators in the same enum?
4. What is the underlying data type used to represent enum constants and enum variables in C?
5. Which of these best describes the primary benefit of using enum instead of plain integer literals?
Was this page helpful?
You May Also Like
Constants in C
Understand C constants: literals, #define, const keyword, enumeration constants, and how to use each correctly.
switch Statement in C
Understand the C switch statement — syntax, fall-through, break, and default rules — with a correct example and common exam traps.
typedef in C
Understand C's typedef keyword to create readable type aliases for structs, pointers, and primitive types.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics