📖 Scenario: You are creating a simple program to represent the days of the week using an enum. This helps to give meaningful names to numbers, making your code easier to read and understand.
🎯 Goal: Build a program that defines an enum for the days of the week, assigns a day to a variable, and prints the numeric value of that day.
📋 What You'll Learn
Define an enum named Day with days from Monday to Sunday
Create a variable of type Day and assign it the value Wednesday
Print the numeric value of the assigned day using printf
💡 Why This Matters
🌍 Real World
Enums are used in programs to give meaningful names to sets of related values, like days, states, or options, making code easier to read and maintain.
💼 Career
Understanding enums is important for writing clear and maintainable code in many programming jobs, especially in systems programming, embedded systems, and application development.
Progress0 / 4 steps
1
Define the enum for days of the week
Write an enum named Day with these exact members: Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, and Sunday.
C
Hint
Use the enum keyword followed by the name Day and list the days inside curly braces separated by commas.
2
Create a variable of type Day and assign Wednesday
Declare a variable named today of type Day and assign it the value Wednesday.
C
Hint
Use the syntax enum Day today = Wednesday; to declare and assign the variable.
3
Print the numeric value of the day
Use printf to print the numeric value of the variable today. Use the format specifier %d.
C
Hint
Wrap your code inside int main() and use printf("%d\n", today); to print the value.
4
Run the program to display the day number
Complete the program by adding the main function if not already present, and run it to display the numeric value of today. The output should be 2.
C
Hint
Make sure your main function prints the value of today and returns 0.
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
Step 1: Understand enum usage
An enum groups related integer constants under meaningful names.
Step 2: Identify the purpose
This helps make code easier to read and maintain by replacing numbers with names.
Final Answer:
To give names to integer constants for better code readability -> Option A
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
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.
Step 2: Check each option
enum Color { RED, GREEN, BLUE }; uses correct syntax: enum Color { RED, GREEN, BLUE };. Others use invalid symbols or assignment.
Final Answer:
enum Color { RED, GREEN, BLUE }; -> Option C
Quick Check:
Correct enum syntax uses braces and commas [OK]
Hint: Use braces {} and commas for enum values [OK]