C Enumerations

Intermediate
6 min read
Updated: Nov 2026
1 example

What Is Enumerations?

An enumeration defines named integer constants. Values start at 0 and increase by one unless you assign them. Enum constants are int constants and make code such as switch statements self-documenting. Wrap them in a typedef for a convenient type name.

Remember
enum Color { RED, GREEN = 5, BLUE };   /* 0, 5, 6 */
enum Color c = GREEN;
typedef enum { OFF, ON } Switch;

Cheat Sheet

Keep these facts handy while you work with Enumerations in C.

TopicDetail
Default valuesStart at 0, add 1 each
ExplicitNAME = value
TypeConstants are int
With switchCompilers can warn about missing cases

C Examples

Compile each example with gcc file.c -std=c11 -Wall -o out. Next: typedef.

Example 1 — Colors and a switch

Enumerators make branches readable.

C
#include <stdio.h>

enum Color { RED, GREEN = 5, BLUE };

static const char *name(enum Color c) {
    switch (c) {
        case RED:   return "Red";
        case GREEN: return "Green";
        case BLUE:  return "Blue";
    }
    return "Unknown";
}

int main(void) {
    printf("RED=%d GREEN=%d BLUE=%d\n", RED, GREEN, BLUE);
    enum Color pick = GREEN;
    printf("Chosen: %s\n", name(pick));
    return 0;
}

How It Works

  1. RED is 0, GREEN is set to 5, and BLUE continues at 6.
  2. The switch uses names instead of magic numbers.
  3. Compile with -Wall to be warned about unhandled enum values.

Edge Cases & Pitfalls

Check these before relying on Enumerations in real programs.

scope

Global enumerator names

Enumerators share the surrounding scope, so two enums cannot both define RED. Prefix names, like COLOR_RED.

range

Out-of-range values

C does not stop you from assigning (enum Color)99. Validate input.

Key Takeaways

  • Names: replace magic numbers.
  • Values: start at 0 unless set.
  • Prefix: avoid name clashes.

One line: An enumeration defines named integer constants.

Frequently Asked Questions

An enum is a user-defined type consisting of named integer constants, which improves readability over raw numbers.
Yes. Two enumerators can share the same value, though that is rarely a good idea.
Continue with "C typedef" next. Create shorter or clearer names for existing types. The C Structures, Unions and Enums hub lists every topic in order.

Next: typedef

Create shorter or clearer names for existing types.

Continue →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

11 people found this page helpful