0
0
Cprogramming~10 mins

Enum usage in C - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Enum usage
Define enum with names and values
Declare variable of enum type
Assign enum value to variable
Use variable in code (e.g., print or condition)
Program runs with enum values as integers
This flow shows how you define an enum, assign its values to variables, and use them in your program.
Execution Sample
C
enum Color { RED, GREEN, BLUE };
enum Color c = GREEN;
printf("Color value: %d\n", c);
Defines an enum Color, assigns GREEN to variable c, then prints its integer value.
Execution Table
StepActionVariableValueOutput
1Define enum Color with RED=0, GREEN=1, BLUE=2---
2Declare variable c of type Colorcundefined-
3Assign GREEN to cc1-
4Print value of cc1Color value: 1
5Program ends---
💡 Program ends after printing the enum variable's integer value.
Variable Tracker
VariableStartAfter Step 3Final
cundefined11
Key Moments - 2 Insights
Why does the printed value show a number instead of the name GREEN?
In C, enums are stored as integers. The name GREEN corresponds to the integer 1. The printf with %d prints the integer value, not the name. See execution_table step 4.
What happens if I don't assign a value to enum members?
By default, the first enum member is 0, and each next member increments by 1 automatically. This is shown in execution_table step 1 where RED=0, GREEN=1, BLUE=2.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of variable c after step 3?
A2
B0
C1
Dundefined
💡 Hint
Check the 'Value' column for variable c at step 3 in the execution_table.
At which step does the program print the output?
AStep 4
BStep 3
CStep 2
DStep 5
💡 Hint
Look at the 'Output' column in the execution_table to find when output occurs.
If you change GREEN to 5 in the enum definition, what will be the value of c after assignment?
A1
B5
C0
D2
💡 Hint
Changing enum member value changes the integer assigned to variable c, see variable_tracker for how values update.
Concept Snapshot
enum Name { MEMBER1, MEMBER2, ... };
Members default to 0,1,2... unless assigned.
Declare variable: enum Name var;
Assign: var = MEMBER2;
Enum values are integers in C.
Use %d to print enum variable value.
Full Transcript
This example shows how to use enums in C. First, you define an enum with named constants. Each name has an integer value starting at 0 by default. Then you declare a variable of that enum type. You assign one of the enum names to the variable. When you print the variable with %d, it shows the integer value of the enum member. This helps organize related constants with readable names instead of just numbers.