✅ What are enum and typedef in C?
They are user-defined data types in C that help improve code readability, structure, and safety:
Feature | Purpose |
|---|---|
| Creates named constants for integer values |
| Creates aliases (shorter names) for data types (like |
🔹 ENUM: Enumeration Type
🎯 Why Do We Need Enum?
Imagine you're writing a program to handle days of the week:
int day = 3;
if (day == 3) {
printf("It's Wednesday!\\n");
}❌ Problem: It’s hard to tell what 3 means here. This is known as a magic number — it's not self-explanatory and hard to maintain.
✅ Solution: Use an enum to assign meaningful names to numbers.
📦 Syntax of enum
enum EnumName {
VALUE1,
VALUE2,
VALUE3
};By default:
First value =
0Next =
1,2, and so on...
🧪 Example: Days of the Week
#include <stdio.h>
enum Day { MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY };
int main() {
enum Day today = WEDNESDAY;
printf("Today is day number: %d\\n", today); // Output: 2
return 0;
}🔎 Internally:
Name | Value |
|---|---|
MONDAY | 0 |
TUESDAY | 1 |
WEDNESDAY | 2 |
THURSDAY | 3 |
FRIDAY | 4 |
🎛️ Custom Starting Values
enum Status {
SUCCESS = 200,
CLIENT_ERROR = 400,
SERVER_ERROR = 500
};Even mix values:
enum Mixed {
A = 3,
B, // 4
C = 10,
D // 11
};🔍 Memory & Type
Enums are stored as integers (usually
int, i.e., 4 bytes).They don't create new types, just labels for constants.
You can still assign values like:
enum Day d = 5; // Not type-safe! No compile-time error⚠️ C does not enforce type-checking for enums (unlike C++).
🧠 Best Practices
Use enums when values are mutually exclusive and finite.
Prefer enums over
#defineconstants for clarity.Don’t assign floating-point or character values in enums (only integers are valid).
🔸 TYPEDEF: Type Alias in C
typedef is a keyword in C used to create a new name (alias) for an existing data type.
It does not create a new type — it simply gives a new identifier to an existing one to improve readability, reusability, and ease of maintenance in your code.
🎯 Why Use Typedef?
Some data types are too long to write repeatedly:
unsigned long long int bigNumber;Or
struct Student {
int roll;
char name[20];
};Wouldn't it be nice to simplify them? typedef does that!
🧾 Syntax of typedef
typedef existing_type new_type_name;💡 Example 1: Typing Simpler Names
typedef unsigned long long int bigNum;
bigNum number = 10000000000;💡 Example 2: For Structs
Without typedef:
struct Point {
int x;
int y;
};
struct Point p1;With typedef:
typedef struct {
int x;
int y;
} Point;
Point p1;✅ Cleaner, no need to write struct again and again!
⚙️ Advanced: Function Pointer with typedef
Without typedef:
void (*funcPtr)(int);With typedef:
typedef void (*FunctionType)(int);
FunctionType f;This is VERY useful in real-world C applications, especially in embedded programming, drivers, or OS code.
💥 Combining typedef and enum
typedef enum {
RED,
GREEN,
BLUE
} Color;
Color c1 = GREEN;✅ Now you can declare variables of enum type without writing enum every time.
🔬 Memory & Compilation Notes
Feature | Enum | Typedef |
|---|---|---|
Purpose | Set of related named constants | Alias for existing data type |
Type | Stored as | Doesn't create new type, just a name |
Use Case | State machines, flags, error codes | Simplify long data type names |
🛠️ Real World Use Cases
Feature | Real-world Usage Example |
|---|---|
| State machines, HTTP status codes, traffic lights |
| Networking structs, low-level drivers, portability |
| OS flags, device driver states |
🧪 Hands-On Practice
🧩 Program: Enum with Switch-Case
#include <stdio.h>
typedef enum {
SMALL = 1,
MEDIUM = 2,
LARGE = 3
} Size;
int main() {
Size pizza = LARGE;
switch(pizza) {
case SMALL: printf("You chose SMALL size.\\n"); break;
case MEDIUM: printf("You chose MEDIUM size.\\n"); break;
case LARGE: printf("You chose LARGE size.\\n"); break;
}
}