0Pricing
C Academy · Lesson

Enum Values and Ranges

Control underlying integers.

Enum Values and Ranges is a free C Academy lesson on CoddyKit — lesson 2 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.

Assigning Explicit Values

You are not stuck with the default 0, 1, 2 sequence. You can assign any integer to a name using =.

This is useful when the numbers must match an external system, like HTTP status codes.

enum Http { OK = 200, NOT_FOUND = 404, ERROR = 500 };

Printing Custom Values

Once assigned, those exact numbers are what get stored and printed.

Here the enum names map directly to standard HTTP codes.

#include <stdio.h>
enum Http { OK = 200, NOT_FOUND = 404, ERROR = 500 };
int main(void) {
    printf("%d %d\n", OK, NOT_FOUND);
    return 0;
}

Mixing Explicit and Default

If you set some values and leave others blank, each blank name continues counting from the previous value.

So a name right after = 10 becomes 11, then 12, and so on.

#include <stdio.h>
enum N { A = 10, B, C, D = 100, E };
int main(void) {
    printf("%d %d %d %d %d\n", A, B, C, D, E);
    return 0;
}

Starting at One

A common trick is to make the first value 1 instead of 0, so the numbering matches human counting.

Here MON is 1, making the days run 1 through 7.

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

Duplicate Values Are Allowed

Two enum names can share the same integer value. C does not require them to be unique.

This is sometimes used to create aliases for the same concept.

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

Negative Values

Enum values can be negative too. They are signed integers.

This can express error codes below zero alongside a neutral or positive range.

#include <stdio.h>
enum Temp { COLD = -1, NORMAL = 0, HOT = 1 };
int main(void) {
    printf("%d %d %d\n", COLD, NORMAL, HOT);
    return 0;
}

A COUNT Sentinel Trick

If you list items starting at 0 and add a final name, that last name equals the number of items.

This COUNT trick gives you the size automatically, even after you add new items.

#include <stdio.h>
enum Fruit { APPLE, PEAR, KIWI, FRUIT_COUNT };
int main(void) {
    printf("%d fruits\n", FRUIT_COUNT);
    return 0;
}

Enums as Array Indices

Because enum values are integers, they make excellent array indices.

Combined with the COUNT trick, you can size an array exactly and index it with readable names.

#include <stdio.h>
enum Fruit { APPLE, PEAR, KIWI, FRUIT_COUNT };
int main(void) {
    int stock[FRUIT_COUNT] = {5, 3, 8};
    printf("%d\n", stock[PEAR]);
    return 0;
}

The Underlying Type

In standard C, enum values are stored in an integer type chosen by the compiler, typically int.

The type is wide enough to hold every value you define, so large or negative constants still fit.

#include <stdio.h>
enum Big { SMALL = 0, LARGE = 1000000 };
int main(void) {
    printf("%d\n", LARGE);
    return 0;
}

No Built-in Range Checking

C does not stop you from assigning an out-of-list value to an enum variable.

The assignment below compiles and runs. It is your responsibility to keep values valid.

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

Looping Over a Range

When an enum runs from 0 to a COUNT sentinel, you can loop over every value with a simple for.

This iterates through each defined item up to, but not including, the count.

#include <stdio.h>
enum Fruit { APPLE, PEAR, KIWI, FRUIT_COUNT };
int main(void) {
    for (int f = APPLE; f < FRUIT_COUNT; f++)
        printf("%d ", f);
    printf("\n");
    return 0;
}

Quick Check

Reason about mixed enum values.

Recap

You can assign explicit enum values, mix them with defaults, repeat values, and use negatives.

The COUNT sentinel trick auto-tracks list size, and enums work great as array indices. Remember: C does no range checking, so validate values yourself.

Frequently asked questions

Is the “Enum Values and Ranges” lesson free?

Yes — the full text of “Enum Values and Ranges” 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 “Enum Values and Ranges”?

Control underlying integers. 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 2 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Enum Values and Ranges” 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