Bird
Raised Fist0
Cprogramming~10 mins

Enum usage in C - Step-by-Step Execution

Choose your learning style10 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.

Practice

(1/5)
1. What is the main purpose of using enum in C programming?
easy
A. To give names to integer constants for better code readability
B. To create floating-point variables
C. To define functions
D. To allocate memory dynamically

Solution

  1. Step 1: Understand enum usage

    An enum groups related integer constants under meaningful names.
  2. Step 2: Identify the purpose

    This helps make code easier to read and maintain by replacing numbers with names.
  3. Final Answer:

    To give names to integer constants for better code readability -> Option A
  4. Quick Check:

    Enum = Named integer constants [OK]
Hint: Enums name numbers to improve code clarity [OK]
Common Mistakes:
  • Thinking enums create variables or functions
  • Confusing enums with dynamic memory
  • Assuming enums hold floating-point values
2. Which of the following is the correct syntax to declare an enum named Color with values RED, GREEN, and BLUE?
easy
A. enum Color: RED, GREEN, BLUE;
B. enum Color = { RED, GREEN, BLUE };
C. enum Color { RED, GREEN, BLUE };
D. enum Color ( RED, GREEN, BLUE );

Solution

  1. Step 1: Recall enum declaration syntax

    In C, enums are declared using the keyword enum, followed by the name and braces with values separated by commas.
  2. Step 2: Check each option

    enum Color { RED, GREEN, BLUE }; uses correct syntax: enum Color { RED, GREEN, BLUE };. Others use invalid symbols or assignment.
  3. Final Answer:

    enum Color { RED, GREEN, BLUE }; -> Option C
  4. Quick Check:

    Correct enum syntax uses braces and commas [OK]
Hint: Use braces {} and commas for enum values [OK]
Common Mistakes:
  • Using '=' instead of braces
  • Using parentheses instead of braces
  • Using colon ':' which is invalid in C enums
3. What will be the output of this C code?
enum Day { SUN, MON, TUE = 5, WED, THU };
#include 
int main() {
    printf("%d %d %d %d %d", SUN, MON, TUE, WED, THU);
    return 0;
}
medium
A. 1 2 5 6 7
B. 5 6 7 8 9
C. 0 1 2 3 4
D. 0 1 5 6 7

Solution

  1. Step 1: Understand enum value assignment

    By default, enums start at 0 and increase by 1. Here, SUN=0, MON=1. TUE is explicitly set to 5.
  2. Step 2: Calculate following values

    After TUE=5, WED becomes 6, and THU becomes 7 automatically.
  3. Final Answer:

    0 1 5 6 7 -> Option D
  4. Quick Check:

    Explicit enum values override counting [OK]
Hint: Explicit enum values reset counting from that point [OK]
Common Mistakes:
  • Assuming all values auto-increment ignoring explicit assignments
  • Starting enum values at 1 by default
  • Confusing enum names with strings
4. Identify the error in this enum declaration:
enum Status {
    OK = 1,
    ERROR,
    WARNING = "3"
};
medium
A. Missing semicolon after enum declaration
B. Enum values must be integers, not strings
C. Enum names cannot be uppercase
D. ERROR cannot follow OK without explicit value

Solution

  1. Step 1: Check enum value types

    Enum values in C must be integer constants. Here, WARNING is assigned a string "3", which is invalid.
  2. Step 2: Verify other syntax

    Semicolon is present, uppercase names are allowed, and ERROR can auto-increment from OK=1.
  3. Final Answer:

    Enum values must be integers, not strings -> Option B
  4. Quick Check:

    Enum values = integers only [OK]
Hint: Enum values must be integer constants [OK]
Common Mistakes:
  • Assigning strings to enum values
  • Thinking enum names must be lowercase
  • Forgetting semicolon after enum
5. Given this enum and code snippet, what is the output?
enum Flags { FLAG_A = 1 << 0, FLAG_B = 1 << 1, FLAG_C = 1 << 2 };
#include 
int main() {
    int combined = FLAG_A | FLAG_C;
    if (combined & FLAG_B) {
        printf("Flag B set\n");
    } else {
        printf("Flag B not set\n");
    }
    return 0;
}
hard
A. Flag B not set
B. Flag B set
C. Compilation error
D. Flag A and C set

Solution

  1. Step 1: Understand bitwise enum values

    FLAG_A = 1 (binary 001), FLAG_B = 2 (010), FLAG_C = 4 (100). Combined = FLAG_A | FLAG_C = 1 | 4 = 5 (binary 101).
  2. Step 2: Check if FLAG_B is set in combined

    combined & FLAG_B = 5 & 2 = 0 (binary 101 & 010 = 000), so FLAG_B is not set.
  3. Final Answer:

    Flag B not set -> Option A
  4. Quick Check:

    Bitwise AND zero means flag not set [OK]
Hint: Use bitwise AND to check if flag is set [OK]
Common Mistakes:
  • Assuming combined flags include all bits
  • Confusing bitwise OR and AND
  • Expecting compilation error due to bit shifts
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard