Menu
Coddy logo textTech

enum for Named Constants

Part of the Logic & Flow section of Coddy's C journey — lesson 55 of 63.

When writing C programs, you often need to work with sets of related constant values. For example, you might need to represent days of the week, months of the year, or different states in a program. Instead of using raw numbers like 0, 1, 2, you can use enum to create meaningful names for these constants.

An enumerated type, or enum, allows you to define a group of named integer constants. This makes your code much more readable and easier to maintain. Here's the basic syntax:

enum DayOfWeek {
    MONDAY,
    TUESDAY,
    WEDNESDAY,
    THURSDAY,
    FRIDAY,
    SATURDAY,
    SUNDAY
};

By default, the first name gets the value 0, the second gets 1, and so on. So MONDAY equals 0, TUESDAY equals 1, and SUNDAY equals 6. This approach is much clearer than remembering that day 0 means Monday or day 6 means Sunday.

Using enum makes your code self-documenting and helps prevent errors that can occur when using magic numbers throughout your program.

You can declare variables of enum type and assign enum values to them:

enum DayOfWeek today;
today = MONDAY;  // assigns 0 to today
challenge icon

Challenge

Easy

Create a C program that defines an enum for traffic light colors and demonstrates its usage. Your program should:

  1. Define an enum named TrafficLight with the following constants in this exact order:
    • RED
    • YELLOW
    • GREEN
  2. In the main function:
    • Declare a variable of type enum TrafficLight named currentLight
    • Read an integer from input representing the traffic light state (0, 1, or 2)
    • Assign the corresponding enum value to currentLight based on the input:
      • If input is 0, assign RED
      • If input is 1, assign YELLOW
      • If input is 2, assign GREEN
    • Print the current light status in this exact format: Current light: [enum_name]
    • Print the numeric value of the current light in this exact format: Numeric value: [number]
    • Determine and print the action based on the current light:
      • If RED: print Action: Stop
      • If YELLOW: print Action: Caution
      • If GREEN: print Action: Go
    • Calculate and print the next light in the sequence:
      • If current light is RED (0), next is GREEN (2)
      • If current light is YELLOW (1), next is RED (0)
      • If current light is GREEN (2), next is YELLOW (1)
    • Print the next light in this exact format: Next light: [enum_name]

This challenge demonstrates how enum constants make code more readable than using raw numbers. Instead of remembering that 0 means red, 1 means yellow, and 2 means green, you can use meaningful names like RED, YELLOW, and GREEN. You'll practice defining an enum, using enum constants in assignments and comparisons, and accessing the underlying integer values of enum constants.

Cheat sheet

An enum allows you to define a group of named integer constants, making code more readable than using raw numbers:

enum DayOfWeek {
    MONDAY,
    TUESDAY,
    WEDNESDAY,
    THURSDAY,
    FRIDAY,
    SATURDAY,
    SUNDAY
};

By default, the first name gets value 0, the second gets 1, and so on. So MONDAY equals 0, TUESDAY equals 1, etc.

You can declare variables of enum type and assign enum values to them:

enum DayOfWeek today;
today = MONDAY;  // assigns 0 to today

Try it yourself

#include <stdio.h>

// TODO: Define the TrafficLight enum here

int main() {
    // Read input
    int input;
    scanf("%d", &input);
    
    // TODO: Declare currentLight variable and assign enum value based on input
    
    // TODO: Print current light status
    
    // TODO: Print numeric value
    
    // TODO: Print action based on current light
    
    // TODO: Calculate and print next light
    
    return 0;
}
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Logic & Flow