Skip to content

Latest commit

 

History

History
85 lines (61 loc) · 1.42 KB

File metadata and controls

85 lines (61 loc) · 1.42 KB

Module 08: Structures and Unions

Organize complex data with structures and unions!

What You'll Learn

  1. Defining structures
  2. Nested structures
  3. Arrays of structures
  4. Pointers to structures
  5. Unions and bit fields
  6. typedef and enum

Structures

#include <stdio.h>
#include <string.h>

struct Student {
 char name[50];
 int age;
 float gpa;
};

int main() {
 struct Student s1;
 strcpy(s1.name, "Alice");
 s1.age = 20;
 s1.gpa = 3.8;

 printf("Name: %s, Age: %d, GPA: %.2f\n", s1.name, s1.age, s1.gpa);

 return 0;
}

Typedef

typedef struct {
 int x;
 int y;
} Point;

Point p1 = {10, 20};

Unions

union Data {
 int i;
 float f;
 char str[20];
};

union Data data;
data.i = 10;
printf("%d\n", data.i);
data.f = 220.5;
printf("%.2f\n", data.f);

Next Module

Module 09: File I/O

Files in this Module

Each source file has a companion markdown file with detailed explanations:

File Description
advanced_structures.c 📄 Advanced Structures demonstration
bitfields_and_unions.c 📄 Bitfields And Unions demonstration
structures_demo.c 📄 Structures Demo demonstration
unions_demo.c 📄 Unions Demo demonstration

Legend

  • 📄 = Detailed explanation available
  • 🐛 = Contains deliberate bugs for learning