100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
C

Enumerations (enum) in C

Learn C enumerations: syntax, default integer values, custom value assignment, and best practices with examples.

Basics & Data TypesBeginner7 min readJul 7, 2026
Analogies

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

c
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

c
#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

text
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

Was this page helpful?

Topics covered

#CProgrammingStudyNotes#Programming#EnumerationsEnumInC#Enumerations#Enum#Syntax#Explanation#StudyNotes#SkillVeris#ExamPrep