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
EasyCreate a C program that demonstrates declaring and using enum variables for a simple task management system. Your program should:
- Define an
enumnamedTaskStatuswith the following constants in this exact order:PENDINGIN_PROGRESSCOMPLETEDCANCELLED
- In the main function:
- Declare three variables of type
enum TaskStatusnamedtask1,task2, andtask3 - 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
- If input is 0, assign
- 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
PENDINGorIN_PROGRESS) in this exact format:Active tasks: [count]
- Declare three variables of type
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;
}This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Logic & Flow
1Pointers Fundamentals
What is a Pointer?Declaring PointersThe Address-Of Operator (&)The Dereference Operator (*)NULL PointersRecap: Pointer Basics4Project: Simple Text Utility
Project OverviewCounting Characters7Structures (structs)
What is a Struct?Declaring a StructCreating Struct VariablesAccessing Struct MembersInitializing StructsRecap: Student Data Struct10Enums and Typedef
enum for Named ConstantsDeclaring and Using EnumsEnums in Switch StatementsUsing typedef for Type Aliasestypedef with StructsRecap: Typedef & Enum Practice2Pointers and Arrays
Array Names as PointersArray Elements - PointersPointer ArithmeticComparing PointersRecap: Pointer Array Traversal5Pointers and Functions
Pass-by-ValuePassing Pointers to FunctionsModifying Vars via PointersA Classic Example: SwapPassing Arrays to FunctionsRecap: Function Pointer Args8Structs and Pointers
Pointers to StructsThe Arrow Operator (->)Passing Structs by ValuePassing Struct PointersDynamic Allocation of StructsRecap: Modifying Struct - Ptr11Final Recap Challenges
Recap: Dynamic String ConcatRecap: Array of StructsRecap: Word Frequency Counter3Character Arrays and Strings
Strings as char ArraysThe Null TerminatorString Input with scanfUsing strlen()Using strcpy()Using strcat()Using strcmp()Recap: Basic String Functions