› Module 9: Structures, Unions & Enumerations › 9.3 Enumerations and typedef
M O D U L E

Module 9: Structures, Unions & Enumerations

Grouping different data types together to model real-world records.

9.1 Structures

A struct bundles variables of different types under one name, letting you model a real-world record (e.g. a Student with a name, roll number, and marks). Each member gets its own separate memory; the compiler may insert padding bytes so members align to their natural byte boundaries, so sizeof(struct) can be larger than the sum of its members' sizes.

struct Student {
  char name[20];
  int roll;
  float marks;
};

struct Student s1 = {"Amit", 1, 89.5};
printf("%s scored %.1f", s1.name, s1.marks);

struct Student { char grade; int marks; }; (4-byte alignment)

grade 1 byte pad (unused) marks 4 bytes (int)

Compiler may insert padding bytes so each member is aligned
to its natural boundary -> struct size is often more than the sum of members.

Struct members occupy separate memory, sometimes with padding for alignment.

9.2 Unions

A union looks like a struct syntactically but its members share the SAME memory location rather than having their own. Its total size equals its largest member, and only one member holds a valid value at any given time — writing to one member overwrites whatever was stored in the others. Unions are used to save memory when only one of several interpretations of data is needed at a time.

union Data { int i; float f; char c[4]; }; // -- all members SHARE the same memory
Single shared block: 4 bytes i, f and c[4] all start at the same address.

Writing to one member overwrites the others; union size = size of its largest member.

All union members overlap the same block of memory.

9.3 Enumerations and typedef

enum creates a set of named integer constants for readability (enum Day {MON, TUE, WED}; — MON is 0, TUE is 1, and so on by default). typedef creates an alias for an existing type, most often used to shorten struct usage: typedef struct {...} Student; lets you write 'Student s1;' instead of 'struct Student s1;'.

enum Day { MON, TUE, WED, THU, FRI, SAT, SUN };
// MON=0, TUE=1, WED=2 ...

typedef struct {
  int id;
  float gpa;
} Student;

Student s1 = {101, 3.95}; // No need for "struct Student"

9.4 Nested Structures and Arrays of Structures

A struct can contain another struct as a member (nested structures), useful for hierarchical data like an Employee containing an Address struct. An array of structures (struct Student class[50];) is the standard way to store many records of the same shape, combining the record-modeling power of structs with the bulk-storage power of arrays.

struct Address {
  char city[30];
  int zip;
};

struct Employee {
  int empId;
  char name[50];
  struct Address addr; // Nested struct
};

struct Employee company[100]; // Array of 100 Employee structs
company[0].empId = 1001;
company[0].addr.zip = 10001;
MODULE COMPREHENSIVE QUIZ

Module 9 Final Quiz

Test your understanding across all 4 topics of this module with interview-grade questions, tricky edge cases, and code output predictions.

Take Module 9 Final Quiz →
Yukti Mascot