SovranCode
HomeCourses C C Enums
This device
Course contentsC Enums · 21 titles

C Fundamentals

C IntroductionC First ProgramC VariablesC Data TypesC OperatorsC Input and Output

Control Flow

C If ElseC For LoopC While Loop

Functions and Collections

C FunctionsC ArraysC Strings

Memory and User-Defined Types

C PointersC StructuresC EnumsC Dynamic Memory

Files, Tooling, and Projects

C File HandlingC PreprocessorC DebuggingC MakefilesC Project: Command-Line Inventory Manager
Learn C Programming21 complete lessons

C Fundamentals

C IntroductionC First ProgramC VariablesC Data TypesC OperatorsC Input and Output

Control Flow

C If ElseC For LoopC While Loop

Functions and Collections

C FunctionsC ArraysC Strings

Memory and User-Defined Types

C PointersC StructuresC EnumsC Dynamic Memory

Files, Tooling, and Projects

C File HandlingC PreprocessorC DebuggingC MakefilesC Project: Command-Line Inventory Manager
PREVIOUS LESSONC Structures
NEXT LESSONC Dynamic Memory
Memory and User-Defined Types 55 min

C Enums

Represent a closed set of integral states with names, not unexplained literals.

What you will leave with

You will declare an enum, switch on named constants, keep a default for untrusted input, and avoid scattering magic numbers through control flow.

Enumerations give states readable names

An enum declares integral constants with domain-specific names. It makes control flow easier to read and lets you avoid unexplained literals such as 3 scattered through your program. Structures still hold related fields; enums name a closed set of states those records can occupy. Continue with records in C Structures.

enum OrderStatus {
  ORDER_PENDING,
  ORDER_PAID,
  ORDER_SHIPPED,
  ORDER_CANCELLED,
};

const char *status_label(enum OrderStatus status) {
  switch (status) {
    case ORDER_PENDING: return "pending";
    case ORDER_PAID: return "paid";
    case ORDER_SHIPPED: return "shipped";
    case ORDER_CANCELLED: return "cancelled";
    default: return "invalid status";
  }
}
C DATA-MODEL RUNNER

Run a named state instead of a magic number

The runner validates the declared ConnectionState names and reports the active enum constant with its current ordinal.

#include <stdio.h>

enum ConnectionState {
  CONNECTION_IDLE,
  CONNECTION_CONNECTING,
  CONNECTION_CONNECTED,
  CONNECTION_FAILED,
};

int main(void) {
  enum ConnectionState state = CONNECTION_CONNECTED;
  printf("State selected\n");
  return 0;
}
TERMINAL OUTPUT
Edit the model, predict the result, then run it.

This safe browser runner executes a deliberately narrow C data-model exercise. It never runs native code: it validates the lesson's enum contract and evaluates only the visible record, enum, or union state.

Try this next: Change state to CONNECTION_IDLE or CONNECTION_FAILED, run it, then reorder the enum members and observe why application code should compare names.

Default values start at zero; names are the contract

The default underlying values begin at zero and increase by one, but your code should rely on named constants—not their current numbers. Assign explicit values only when an external format, protocol, or stored data contract requires stable numeric codes. Reordering members without explicit values will change those numbers.

switch is a natural match for a closed set

A switch on an enum lists each named state. Include break after each case unless you intentionally fall through and comment that decision. C does not require switch to be exhaustive the way some languages do, so a missing case is a logic bug, not a compile error on every compiler.

An enum does not make invalid bytes impossible

C can still receive an unexpected value from a file, network packet, cast, or uninitialized object. Keep a default branch when input can cross a trust boundary, and make the recovery behavior intentional. Enums are often used as the tag beside a union; that tagged-union pattern is taught with structures.

Do not cast unchecked integers into an enum and assume they are valid

Validate first, then store a named constant. A tag that does not match any enumerator is data, not a guaranteed member of the set.

Independent lab: label every state

Declare an enum for a task status, write a function that returns a string label for each name, and include default. Compile locally. Then reorder two members without explicit values and confirm that code comparing names still compiles while any code comparing raw numbers would silently change meaning.

Lesson review

  • An enum names a closed set of integral constants.
  • Compare enumerator names, not assumed ordinals, unless a protocol documents the numbers.
  • switch plus default handles named states and unexpected values.
  • Enums do not reject invalid stored integers by themselves.

Related lessons

  • C Structures — Enums often tag structs and unions.
  • C If Else — switch on an enum is another form of choosing a path.
KNOWLEDGE CHECK

Name the state, not a magic number

Enumerations are integer constants with domain names. They do not automatically reject invalid stored values.

01Why is an enum useful for an order status?
02What are the default values of enum { A, B, C }?
03Why keep a default in a switch on an enum that came from a file?
04Should application code compare ORDER_PAID by its current number?
05Where do enums often appear with unions?
PREVIOUS LESSONC Structures
NEXT LESSONC Dynamic Memory
ON THIS PAGEC EnumsNamed statesValuesswitchUntrusted valuesIndependent labLesson reviewKnowledge checkRelated lessons
Course contents