Menu
Coddy logo textTech

Declaring and Using Enums

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

Now that you've defined an enum, you can create variables of that enum type and assign them meaningful values. This is where enums really shine - instead of working with confusing numbers, you use descriptive names.

To declare a variable of an enum type, you use the same syntax as declaring any other variable:

enum DayOfWeek today;
enum DayOfWeek weekend_day;

You can then assign values to these variables using the named constants from your enum:

today = WEDNESDAY;
weekend_day = SATURDAY;

Compare this to using raw numbers: today = 2; and weekend_day = 5;. The enum version immediately tells you what day you're working with, while the numbers require you to remember what each value represents. This makes your code much more readable and less prone to errors.

challenge icon

Challenge

Easy

Create a C program that demonstrates declaring and using enum variables for a simple task management system. Your program should:

  1. Define an enum named TaskStatus with the following constants in this exact order:
    • PENDING
    • IN_PROGRESS
    • COMPLETED
    • CANCELLED
  2. In the main function:
    • Declare three variables of type enum TaskStatus named task1, task2, and task3
    • Read three integers from input representing the status of each task (0, 1, 2, or 3)
    • Assign the corresponding enum values to each task variable based on the input:
      • If input is 0, assign PENDING
      • If input is 1, assign IN_PROGRESS
      • If input is 2, assign COMPLETED
      • If input is 3, assign CANCELLED
    • Print the status of each task using the enum values in this exact format:
      • Task 1 status: [numeric_value]
      • Task 2 status: [numeric_value]
      • Task 3 status: [numeric_value]
    • Count and print how many tasks are in each status category:
      • Pending tasks: [count]
      • In progress tasks: [count]
      • Completed tasks: [count]
      • Cancelled tasks: [count]
    • Calculate and print the total number of active tasks (tasks that are either PENDING or IN_PROGRESS) in this exact format: Active tasks: [count]

This challenge demonstrates how enum variables make code more readable by using meaningful names instead of raw numbers. You'll practice declaring enum variables, assigning enum values based on input, using enum constants in comparisons, and performing calculations with enum values.

Cheat sheet

To declare variables of an enum type, use the same syntax as declaring any other variable:

enum DayOfWeek today;
enum DayOfWeek weekend_day;

Assign values to enum variables using the named constants from your enum:

today = WEDNESDAY;
weekend_day = SATURDAY;

Using enum names instead of raw numbers makes code more readable and less error-prone. Compare today = WEDNESDAY; with today = 2; - the enum version immediately shows what day you're working with.

Try it yourself

#include <stdio.h>

// TODO: Define the TaskStatus enum here

int main() {
    // Read input for three task statuses
    int status1, status2, status3;
    scanf("%d", &status1);
    scanf("%d", &status2);
    scanf("%d", &status3);
    
    // TODO: Declare three enum TaskStatus variables (task1, task2, task3)
    
    // TODO: Assign enum values based on input
    
    // TODO: Print task statuses
    
    // TODO: Count tasks in each category
    
    // TODO: Calculate and print active tasks count
    
    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