Enum usage in C - Time & Space Complexity
Enums help us name groups of related values in code. When using enums, it's important to see how the program's steps grow as we use them.
We want to know how the program's work changes when enums are involved.
Analyze the time complexity of the following code snippet.
#include <stdio.h>
enum Color { RED, GREEN, BLUE };
void printColor(enum Color c) {
switch(c) {
case RED: printf("Red\n"); break;
case GREEN: printf("Green\n"); break;
case BLUE: printf("Blue\n"); break;
}
}
int main() {
enum Color colors[] = {RED, GREEN, BLUE, RED};
int n = sizeof(colors) / sizeof(colors[0]);
for(int i = 0; i < n; i++) {
printColor(colors[i]);
}
return 0;
}
This code defines an enum for colors and prints each color in an array using a loop and a switch-case.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Looping through the array of colors and calling printColor for each.
- How many times: The loop runs once for each color in the array (n times).
As the number of colors in the array grows, the program prints each one once.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 print calls |
| 100 | About 100 print calls |
| 1000 | About 1000 print calls |
Pattern observation: The work grows directly with the number of colors; doubling colors doubles the work.
Time Complexity: O(n)
This means the program's work grows in a straight line with the number of colors to print.
[X] Wrong: "Using enums makes the program run faster or slower depending on the enum size."
[OK] Correct: Enums are just named numbers; they don't change how many times the program runs loops or prints. The time depends on how many items you process, not on enum details.
Understanding how enums fit into loops and switches helps you explain how your code handles groups of values efficiently. This skill shows you can think about how code grows with input, a key part of writing good programs.
"What if we replaced the array with a linked list of colors? How would the time complexity change?"
Practice
enum in C programming?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 AQuick Check:
Enum = Named integer constants [OK]
- Thinking enums create variables or functions
- Confusing enums with dynamic memory
- Assuming enums hold floating-point values
Color with values RED, GREEN, and BLUE?Solution
Step 1: Recall enum declaration syntax
In C, enums are declared using the keywordenum, 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 CQuick Check:
Correct enum syntax uses braces and commas [OK]
- Using '=' instead of braces
- Using parentheses instead of braces
- Using colon ':' which is invalid in C enums
enum Day { SUN, MON, TUE = 5, WED, THU };
#include
int main() {
printf("%d %d %d %d %d", SUN, MON, TUE, WED, THU);
return 0;
}Solution
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.Step 2: Calculate following values
After TUE=5, WED becomes 6, and THU becomes 7 automatically.Final Answer:
0 1 5 6 7 -> Option DQuick Check:
Explicit enum values override counting [OK]
- Assuming all values auto-increment ignoring explicit assignments
- Starting enum values at 1 by default
- Confusing enum names with strings
enum Status {
OK = 1,
ERROR,
WARNING = "3"
};Solution
Step 1: Check enum value types
Enum values in C must be integer constants. Here, WARNING is assigned a string "3", which is invalid.Step 2: Verify other syntax
Semicolon is present, uppercase names are allowed, and ERROR can auto-increment from OK=1.Final Answer:
Enum values must be integers, not strings -> Option BQuick Check:
Enum values = integers only [OK]
- Assigning strings to enum values
- Thinking enum names must be lowercase
- Forgetting semicolon after enum
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;
}Solution
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).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.Final Answer:
Flag B not set -> Option AQuick Check:
Bitwise AND zero means flag not set [OK]
- Assuming combined flags include all bits
- Confusing bitwise OR and AND
- Expecting compilation error due to bit shifts
Ready to prove it?
Pass the test and this pattern moves unknown
