Enumerations (enum) & Type Definitions (typedef)
Unit 4: Structures, Unions, Enums & Storage Classes • Problem Solving Using C
👨🏫 Professor's Mental Model: Traffic Signal Color Lights & Official Aliases/Nicknames
Agar code me har jagah '0', '1', '2' likhoge toh confusing ho jayega ki 0 ka matlab RED tha ya GREEN (Magic Numbers)! enum un numbers ko human-readable naam de deta hai: RED=0, YELLOW=1, GREEN=2. Aur typedef ek official nickname jaisa hai: jaise 'unsigned long long int' itna lamba naam baar-baar likhne ke bajaye aap uska chota nickname 'u64' rakh dete ho!
Academic Lecture Notes & Solved Study Pages
Unit 4 • Core Concepts, Step-by-Step Proofs & Notebook Solutions
1. Enumerated Data Types (enum): Mechanics & Syntax
enum Day { SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY };
You can explicitly assign custom integer values:
`enum Status { SUCCESS = 200, NOT_FOUND = 404, SERVER_ERROR = 500 };`
If an enumerator is left unassigned, it takes the value of the previous enumerator + 1.
2. The typedef Keyword: Creating Type Aliases
1. Simplifying Primitive Types: `typedef unsigned long int ulong;`
2. Eliminating 'struct' keyword repetition:
```c
typedef struct {
int roll;
char name[20];
} Student;
Student s1, s2; // Clean, no 'struct Student' needed!
```
3. Simplifying Complex Function Pointers:
`typedef int (*MathFunc)(int, int);`
3. Comparative Analysis: typedef vs #define
| Construct | Handled By | Scope Rules | Type Safety | Primary Purpose |
|---|---|---|---|---|
| #define | Preprocessor (Text replacement) | Global file-level, ignores blocks | None (Untyped) | Macro expansion, constant literals |
| const | Compiler | Strict block / local scope | Strict type checking | Creating read-only variable memory cells |
| enum | Compiler | Block or file scope | Type-safe integer constants | Grouping discrete related named states/modes |
| typedef | Compiler | Block or file scope | Strict type alias checking | Creating user-friendly synonyms for complex types |
Interactive Tested Code Example
#include <stdio.h>
// 1. Enumeration for Traffic Light States
typedef enum {
LIGHT_RED = 1,
LIGHT_YELLOW,
LIGHT_GREEN
} TrafficLight;
// 2. typedef with Structure
typedef struct {
int id;
char model[20];
TrafficLight current_state;
} VehicleSensor;
int main(void) {
VehicleSensor sensor1 = {501, "TeslaRadar", LIGHT_RED};
printf("=== ENUM & TYPEDEF STATE MACHINE ===\n");
printf("Sensor ID: %d\n", sensor1.id);
printf("State Code: %d\n", sensor1.current_state);
if (sensor1.current_state == LIGHT_RED) {
printf("Action: STOP THE VEHICLE (Light is Red)\n");
} else if (sensor1.current_state == LIGHT_GREEN) {
printf("Action: SAFE TO PROCEED\n");
}
return 0;
}🎯 University Exam Scoring Blueprint
- In university exams, always contrast typedef vs #define regarding preprocessor text replacement versus compiler type analysis.
- State that enum values are strictly integer constants and cannot be assigned floating-point numbers.
- Demonstrate how typedef simplifies struct variable declarations without repeating the 'struct' keyword.