0Pricing
C Academy · Lesson

Defining Enums

Create readable named values.

Defining Enums is a free C Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

What Is an Enum?

An enumeration (enum) is a user-defined type that groups a set of named integer constants.

Instead of remembering that 0 means Monday and 1 means Tuesday, you give those numbers readable names. This makes code clearer and harder to get wrong.

Basic Syntax

You declare an enum with the enum keyword, a tag name, and a list of names in braces.

Each name becomes a constant you can use anywhere an integer is expected.

enum Weekday { MON, TUE, WED, THU, FRI };

Default Values Start at 0

By default the first name is 0, and each following name is one more than the previous.

So in enum Weekday, MON is 0, TUE is 1, WED is 2, and so on.

#include <stdio.h>
enum Weekday { MON, TUE, WED, THU, FRI };
int main(void) {
    printf("%d %d %d\n", MON, TUE, WED);
    return 0;
}

Declaring an Enum Variable

Once an enum type exists, you can create variables of that type.

The variable holds one of the named values. Under the hood it is just an int.

#include <stdio.h>
enum Weekday { MON, TUE, WED, THU, FRI };
int main(void) {
    enum Weekday today = WED;
    printf("today = %d\n", today);
    return 0;
}

Why Enums Improve Readability

Compare if (state == 2) with if (state == PAUSED).

The second is self-documenting. Enums let the compiler track the meaning while you read clean, named values instead of mystery numbers.

enum Status { RUNNING, PAUSED, STOPPED };

Using Enums with switch

Enums pair naturally with switch statements. Each named value becomes a clear case label.

This is one of the most common real-world uses of enums.

#include <stdio.h>
enum Status { RUNNING, PAUSED, STOPPED };
int main(void) {
    enum Status s = PAUSED;
    switch (s) {
        case RUNNING: printf("running\n"); break;
        case PAUSED:  printf("paused\n");  break;
        case STOPPED: printf("stopped\n"); break;
    }
    return 0;
}

Enums Are Just Integers

Enum constants are interchangeable with integers. You can do arithmetic and comparisons with them.

This flexibility is handy but also means C will not stop you from storing an out-of-range number.

#include <stdio.h>
enum Level { LOW, MID, HIGH };
int main(void) {
    enum Level next = LOW + 1;
    printf("%d\n", next);
    return 0;
}

Anonymous Enums

You can omit the tag name if you only need the constants, not a named type.

This is a quick way to define a group of related integer constants.

#include <stdio.h>
enum { RED, GREEN, BLUE };
int main(void) {
    printf("%d %d %d\n", RED, GREEN, BLUE);
    return 0;
}

Naming Conventions

By convention, enum constant names are written in UPPER_CASE, like macros.

This signals to readers that the value is a fixed constant. The tag name often uses CamelCase or PascalCase.

enum TrafficLight { LIGHT_RED, LIGHT_YELLOW, LIGHT_GREEN };

Combining with typedef

Writing enum Weekday everywhere is verbose. A typedef lets you use a short name.

After the typedef you can declare variables as Weekday today; without the enum keyword.

#include <stdio.h>
typedef enum { MON, TUE, WED } Weekday;
int main(void) {
    Weekday d = TUE;
    printf("%d\n", d);
    return 0;
}

Scope of Enum Constants

Enum constant names share the same namespace as ordinary identifiers in their scope.

That means two enums in the same scope cannot reuse the same constant name. Choose distinct names to avoid clashes.

enum A { OPEN, CLOSED };
enum B { LOCKED, UNLOCKED };

Quick Check

Test your understanding of enum defaults.

Recap

An enum defines named integer constants. The first is 0 and each following name is one greater unless you say otherwise.

Enums make code readable, pair well with switch, and can be shortened with typedef. Next we will explore custom values and ranges.

Frequently asked questions

Is the “Defining Enums” lesson free?

Yes — the full text of “Defining Enums” is free to read here on the web, and the C Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C Academy course, upgrade to CoddyKit PRO.

What will I learn in “Defining Enums”?

Create readable named values. You practise C Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start C Academy?

No prior experience is required. C Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Defining Enums” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this C Academy lesson?

Yes. Every C Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Defining Enums
  2. Enum Values and Ranges
  3. const Variables
  4. #define Constants
← Back to C Academy