📖 Scenario: Imagine you are creating a simple program to manage traffic light colors. Each color has a specific meaning and you want to use clear names instead of numbers to represent them.
🎯 Goal: You will create an enumeration to represent traffic light colors and use it in a program to print the meaning of each color.
📋 What You'll Learn
Create an enumeration called TrafficLight with values RED, YELLOW, and GREEN
Create a variable of type TrafficLight and assign it the value RED
Use a switch statement to print the meaning of the current traffic light color
Print the output to the console
💡 Why This Matters
🌍 Real World
Enums are used in many programs to represent fixed sets of options like states, modes, or categories, making code clearer and less error-prone.
💼 Career
Understanding enums is important for writing maintainable and readable code in C programming jobs, especially in embedded systems, game development, and system software.
Progress0 / 4 steps
1
Create the enumeration for traffic light colors
Write an enumeration called TrafficLight with the values RED, YELLOW, and GREEN.
C
Hint
Use the enum keyword followed by the name TrafficLight and list the colors inside curly braces.
2
Create a variable of type TrafficLight and assign RED
Create a variable called currentLight of type TrafficLight and assign it the value RED.
C
Hint
Declare the variable with the enum type and assign RED using the equals sign.
3
Use a switch statement to print the meaning of the traffic light
Write a switch statement using the variable currentLight to print "Stop" for RED, "Ready" for YELLOW, and "Go" for GREEN.
C
Hint
Use switch with case labels for each color and printf to print the messages.
4
Print the output
Add the necessary #include directive and write a main function that runs the code and prints the output.
C
Hint
Remember to include stdio.h and write a main function that returns 0.
Practice
(1/5)
1. Why are enumerations (enums) used in C programming?
easy
A. To give meaningful names to a set of related integer constants
B. To create floating-point variables
C. To allocate memory dynamically
D. To perform input/output operations
Solution
Step 1: Understand the purpose of enums
Enums assign names to integer values, making code easier to read and maintain.
Step 2: Compare with other options
Options B, C, and D describe unrelated tasks like floating-point variables, memory allocation, and I/O operations.
Final Answer:
To give meaningful names to a set of related integer constants -> Option A
Quick Check:
Enums = Named integer constants [OK]
Hint: Enums name numbers for clarity and safety [OK]
Common Mistakes:
Thinking enums create new data types like floats
Confusing enums with memory management
Assuming enums handle input/output
2. Which of the following is the correct way to declare an enumeration in C?
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
Step 1: Recall enum declaration syntax
In C, enums are declared using the syntax: enum Name { list_of_constants };
Step 2: Check each option
enum Color { RED, GREEN, BLUE }; matches the correct syntax. Options B, C, and D use invalid assignment or brackets.
Final Answer:
enum Color { RED, GREEN, BLUE }; -> Option A
Quick Check:
Correct enum syntax uses braces {} [OK]
Hint: Use braces {} to list enum values [OK]
Common Mistakes:
Using = instead of braces
Using parentheses or brackets instead of braces
Missing semicolon at the end
3. What will be the output of the following C code?
enum Day { MON=1, TUE, WED };
int main() {
enum Day today = TUE;
printf("%d", today);
return 0;
}
medium
A. 0
B. 2
C. 1
D. 3
Solution
Step 1: Understand enum value assignment
MON is assigned 1 explicitly, so TUE becomes 2, WED becomes 3 automatically.
Step 2: Identify the value of 'today'
Variable today is set to TUE, which has the value 2.
Final Answer:
2 -> Option B
Quick Check:
Enum auto-increments from assigned start [OK]
Hint: Enum values auto-increment unless set [OK]
Common Mistakes:
Assuming enum starts at 0 when explicitly set
Confusing enum names with values
Forgetting auto-increment behavior
4. Identify the error in this enum declaration:
enum Status {
OK = 0,
ERROR = 1,
UNKNOWN = 1
};
medium
A. Duplicate enum values are not allowed
B. Enum values must start from 0
C. No error, this is valid code
D. Enum names cannot be uppercase
Solution
Step 1: Check enum value rules
In C, enum values can be duplicated; it's allowed to have multiple names with the same value.
Step 2: Verify other options
Enum values do not have to start at 0, and names can be uppercase. So no syntax error here.
Final Answer:
No error, this is valid code -> Option C
Quick Check:
Duplicate enum values allowed [OK]
Hint: Duplicate enum values are allowed in C [OK]
Common Mistakes:
Thinking duplicate values cause errors
Assuming enum must start at zero
Believing enum names must be lowercase
5. You want to use an enum to represent traffic light colors with values RED=0, YELLOW=5, GREEN=10. Which enum declaration correctly achieves this?
hard
A. enum TrafficLight { RED=0, YELLOW, GREEN=10 };
B. enum TrafficLight { RED=1, YELLOW=5, GREEN=10 };
C. enum TrafficLight { RED=0, YELLOW=5, GREEN };
D. enum TrafficLight { RED=0, YELLOW=5, GREEN=10 };
Solution
Step 1: Assign explicit values to enum members
To get RED=0, YELLOW=5, GREEN=10, each must be assigned explicitly.
Step 2: Analyze each option
enum TrafficLight { RED=0, YELLOW=5, GREEN=10 }; assigns all values explicitly. enum TrafficLight { RED=1, YELLOW=5, GREEN=10 }; assigns RED=1 (not 0), YELLOW=5, GREEN=10. enum TrafficLight { RED=0, YELLOW, GREEN=10 }; assigns RED=0, YELLOW unassigned (would be 1), GREEN=10. enum TrafficLight { RED=0, YELLOW=5, GREEN }; assigns RED=0, YELLOW=5, but GREEN unassigned (would be 6).
Final Answer:
enum TrafficLight { RED=0, YELLOW=5, GREEN=10 }; -> Option D
Quick Check:
Explicit values ensure correct enum numbers [OK]
Hint: Assign all enum values explicitly for specific numbers [OK]
Common Mistakes:
Assuming unassigned enums auto-skip to next explicit value
Forgetting to assign all values explicitly
Expecting enums to auto-increment from last explicit value
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.