C PROGRAMMING PROGRAM • STRUCTURES, UNIONS & ENUMS
Represent Weekdays Using an enum
Learn how to represent weekdays using an enum using a clear C program.
PROBLEM UNDERSTANDING
Input and expected output
Sample input
No input required
Sample output
Weekday number = 5
COMPLETE C PROGRAM
Complete C implementation
#include <stdio.h>
enum Weekday { MONDAY = 1, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY };
int main(void)
{
enum Weekday today = FRIDAY;
switch (today) {
case MONDAY: case TUESDAY: case WEDNESDAY: case THURSDAY: case FRIDAY:
printf("Weekday number = %d\n", today); break;
case SATURDAY: case SUNDAY: puts("It is the weekend."); break;
}
return 0;
}CURRENT STEP
SELECTED LINE
EXPECTED OUTPUT FOR THE SAMPLE
Weekday number = 5
Step 0 of 0
PROGRAM EXPLANATION
Algorithm and explanation
- Read the required input values.
- Use meaningful enum names instead of unexplained integer constants.
- Display the computed result.
Use meaningful enum names instead of unexplained integer constants.
EFFICIENCY
Time and space complexity
Time complexity
O(1)
Auxiliary space
O(1)
DEBUGGING CHECKLIST
Common mistakes
Check this
Use the correct format specifier for every variable.
Check this
Initialize variables before using their values.
Check this
Check braces, semicolons and input order carefully.
Try it yourself
Practice: Run the program with the sample input, predict its output, and then test one boundary case of your own.
