An enum helps you give names to a set of related numbers. This makes your code easier to read and understand.
Enum declaration
Introduction
Syntax
C
enum EnumName {
VALUE1,
VALUE2,
VALUE3
};Each name inside the enum represents an integer starting from 0 by default.
You can assign specific numbers to names if you want.
Examples
C
enum Day {
SUNDAY,
MONDAY,
TUESDAY
};C
enum Color { RED = 1, GREEN = 2, BLUE = 4 };
C
enum Status { SUCCESS = 0, ERROR = -1 };
Sample Program
This program uses an enum to represent traffic light colors. It prints a message based on the current light.
C
#include <stdio.h> enum TrafficLight { RED, YELLOW, GREEN }; int main() { enum TrafficLight light = RED; if (light == RED) { printf("Stop\n"); } else if (light == YELLOW) { printf("Get ready\n"); } else { printf("Go\n"); } return 0; }
Important Notes
Enum values are integers and can be used in comparisons and switches.
If you don't assign numbers, they start at 0 and increase by 1.
You can mix assigned and unassigned values; unassigned continue counting from the last assigned.
Summary
Enums give names to numbers to make code clearer.
Use enums when you have a fixed set of related options.
You can assign specific numbers or let them count automatically.
Practice
1. What is the main purpose of using
enum in C programming?easy
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 BQuick 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
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 AQuick 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?
enum Day { SUN, MON, TUE = 5, WED, THU };
#include
int main() {
printf("%d %d %d %d %d", SUN, MON, TUE, WED, THU);
return 0;
}medium
Solution
Step 1: Understand enum value assignment
By default, enum values start at 0 and increment 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 value sets next values incrementally [OK]
Hint: Explicit enum value resets counting from that number [OK]
Common Mistakes:
- Assuming all values increment from zero ignoring explicit assignment
- Thinking WED and THU have same value as TUE
- Starting enum values from 1 by default
4. Identify the error in the following enum declaration:
enum Status {
OK = 1,
ERROR,
WARNING = "2"
};medium
Solution
Step 1: Check enum value types
Enum values must be integer constants. Assigning a string like "2" is invalid.Step 2: Verify other parts
Semicolon is present, uppercase names are allowed, and ERROR can be auto-assigned after OK=1.Final Answer:
Enum values must be integers, not strings -> Option CQuick Check:
Enum values = integers only [OK]
Hint: Enum values must be integers, not strings [OK]
Common Mistakes:
- Using string literals as enum values
- Forgetting semicolon after enum
- Thinking enum names must be lowercase
5. Given the enum declaration:
enum Flags { FLAG_A = 1 << 0, FLAG_B = 1 << 1, FLAG_C = 1 << 2 };
int flags = FLAG_A | FLAG_C;
Which expression correctly checks if FLAG_B is set in flags?hard
Solution
Step 1: Understand bitwise flag checking
To check if a flag is set, use bitwise AND and compare to zero.Step 2: Analyze each option
(flags & FLAG_B) != 0 correctly checks if FLAG_B bit is set by (flags & FLAG_B) != 0. Others use incorrect bitwise operations or comparisons.Final Answer:
(flags & FLAG_B) != 0 -> Option AQuick Check:
Bitwise AND with flag and compare to zero [OK]
Hint: Use bitwise AND and compare to zero to check flags [OK]
Common Mistakes:
- Using bitwise OR instead of AND for checking
- Comparing to wrong values
- Using XOR to check flags
Recommended
Ready to prove it?
Pass the test and this pattern moves unknown
10 questions ~5 mineasy hard
