📖 Scenario: You are creating a simple program to represent days of the week using an enum. This helps you use meaningful names instead of numbers in your code.
🎯 Goal: Learn how to declare an enum in C to represent the days of the week and print one of the days.
📋 What You'll Learn
Declare an enum named Day with the days: Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday.
Create a variable of type Day and assign it the value Wednesday.
Print the integer value of the Day variable.
💡 Why This Matters
🌍 Real World
Enums help represent fixed sets of related values like days, colors, or states in a readable way.
💼 Career
Understanding enums is important for writing clear and maintainable code in C programming jobs.
Progress0 / 4 steps
1
Declare the enum Day
Declare an enum named Day with these exact members in order: Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday.
C
Hint
Use the syntax enum Name { item1, item2, ... }; to declare an enum.
2
Create a variable of type Day
Create a variable named today of type Day and assign it the value Wednesday.
C
Hint
Use enum Day variableName = Value; to create and assign an enum variable.
3
Print the integer value of today
Write a printf statement to print the integer value of the variable today.
C
Hint
Use printf("%d\n", variable); to print an integer value.
4
Run the program to see the output
Run the program and observe the output. It should print the integer value corresponding to Wednesday.
C
Hint
The output should be 3 because Sunday starts at 0 and Wednesday is the fourth item.
Practice
(1/5)
1. What is the main purpose of using enum in C programming?
easy
A. To declare a function prototype
B. To give names to a set of integer constants for better code readability
C. To create a new data type for floating-point numbers
D. To allocate memory dynamically
Solution
Step 1: Understand enum usage
An enum is used to assign names to integer constants, making code easier to read and maintain.
Step 2: Compare other options
Other options describe unrelated concepts: function prototypes, floating-point types, and memory allocation.
Final Answer:
To give names to a set of integer constants for better code readability -> Option B
Quick Check:
Enum = Named integer constants [OK]
Hint: Enums name numbers to make code clearer [OK]
Common Mistakes:
Confusing enums with structs or unions
Thinking enums allocate memory dynamically
Assuming enums are for floating-point types
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
The correct syntax uses curly braces to list values after the enum name without assignment or colons.
Step 2: Check each option
enum Color { RED, GREEN, BLUE }; uses correct syntax: enum Color { RED, GREEN, BLUE };. Others use invalid symbols like '=', '(', or ':'.
Final Answer:
enum Color { RED, GREEN, BLUE }; -> Option A
Quick Check:
Enum syntax = enum Name { values } [OK]
Hint: Use curly braces and no equals sign in enum declaration [OK]
Common Mistakes:
Using '=' instead of braces
Using parentheses instead of braces
Using colons after enum name
3. What will be the output of the following C code?