C Notes
Complete guide to enums in C programming — definition, syntax, automatic and custom values, enum vs #define, use in switch statements, bit flags, practical examples with output.
When your code has a fixed set of named options — days of the week, colors, states, error codes — using raw integers like 0, 1, 2 makes your code unreadable. Enums (enumerated types) let you define meaningful names for these integer constants, making your code self-documenting.
What is an Enum?
An enum is a user-defined type that consists of a set of named integer constants:
enum Day {
SUNDAY, // 0
MONDAY, // 1
TUESDAY, // 2
WEDNESDAY, // 3
THURSDAY, // 4
FRIDAY, // 5
SATURDAY // 6
};By default, the first value is 0, and each subsequent value increments by 1.
Basic Enum Example
Season value: 2 It's autumn!
Custom Enum Values
You can assign specific values to enum constants:
#include <stdio.h>
enum HttpStatus {
OK = 200,
CREATED = 201,
BAD_REQUEST = 400,
NOT_FOUND = 404,
SERVER_ERROR = 500
};
int main() {
enum HttpStatus response = NOT_FOUND;
printf("Status code: %d\n", response);
if (response >= 400) {
printf("Error response!\n");
}
return 0;
}Status code: 404 Error response!
Mixed Custom and Auto Values
enum Priority {
LOW = 1,
MEDIUM, // 2 (auto-increments from 1)
HIGH, // 3
CRITICAL = 10,
EMERGENCY // 11 (auto-increments from 10)
};Enum vs #define — When to Use Which
| Feature | enum | #define |
|---|---|---|
| Type safety | Yes (enum type) | No (just text replacement) |
| Debugging | Shows name in debugger | Shows raw number |
| Scope | Follows C scoping rules | Global (until #undef) |
| Auto-increment | Yes (0, 1, 2...) | No |
| Groups related constants | Yes | Not inherently |
| Can use in switch | Yes (compiler warns if case missed) | Yes |
// #define approach
#define COLOR_RED 0
#define COLOR_GREEN 1
#define COLOR_BLUE 2
// enum approach (preferred)
enum Color { RED, GREEN, BLUE };
// enum is better because:
// 1. Groups constants logically
// 2. Debugger shows "RED" not "0"
// 3. Compiler can warn about missing switch casesEnum with Bit Flags
#include <stdio.h>
enum Permission {
PERM_READ = 1, // 001
PERM_WRITE = 2, // 010
PERM_EXECUTE = 4 // 100
};
int main() {
// Combine permissions with bitwise OR
int user_perms = PERM_READ | PERM_WRITE; // 011 = 3
int admin_perms = PERM_READ | PERM_WRITE | PERM_EXECUTE; // 111 = 7
printf("User permissions: %d\n", user_perms);
printf("Admin permissions: %d\n", admin_perms);
// Check specific permission with AND
if (user_perms & PERM_WRITE) {
printf("User can write\n");
}
if (!(user_perms & PERM_EXECUTE)) {
printf("User cannot execute\n");
}
return 0;
}User permissions: 3 Admin permissions: 7 User can write User cannot execute
Enum in State Machines
Light: RED Light: GREEN Light: YELLOW Light: RED Light: GREEN Light: YELLOW
Enum Size and Type
#include <stdio.h>
enum Small { A, B, C };
enum Large { X = 1000000000 };
int main() {
printf("sizeof(enum Small): %zu\n", sizeof(enum Small));
printf("sizeof(enum Large): %zu\n", sizeof(enum Large));
printf("sizeof(int): %zu\n", sizeof(int));
// Enums are always int-sized in C
return 0;
}sizeof(enum Small): 4 sizeof(enum Large): 4 sizeof(int): 4
In C, enums are always stored as int (4 bytes typically), regardless of the values they hold.
Practical Example: Menu System
=== Student Management === 1. Add Student 2. Delete Student 3. Search Student 4. Display All 5. Exit Enter choice: 3 Searching...
Interview Questions
Q1: What is the default value of the first enum constant?
The first constant is 0, and each subsequent constant increments by 1 unless explicitly assigned. You can override any constant with a custom value.
Q2: Can two enum constants have the same value?
Yes. enum { A = 1, B = 1 } is valid. This is sometimes used for aliases: enum { SUCCESS = 0, OK = 0 }.
Q3: What is the size of an enum variable in C?
In C, enum variables are always stored as int (typically 4 bytes), regardless of how few or small the values are. Some compilers offer extensions to use smaller types.
Q4: Can you use enums with arrays?
Yes, and it's a great pattern for readable code:
Q5: Why prefer enum over #define for related constants?
Enums provide type safety (compiler warnings), debugger visibility (names vs numbers), logical grouping, and auto-incrementing values. #define has no type, no scope, and no grouping.
Summary
- Enums define a set of named integer constants
- Default values start at 0 and auto-increment
- Custom values can be assigned to any constant
- Prefer enums over
#definefor groups of related constants - Enums are excellent for switch statements, state machines, and flags
- Combine with bit flags using powers of 2 for permission systems
- In C, enum size is always
sizeof(int)
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for Enums in C – Enumerated Data Types.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this C Programming topic.
Search Terms
c-programming, c programming, programming, structures, and, unions, enums, enums in c – enumerated data types
Related C Programming Topics