C in 100 Seconds: Structs — Group Your Data | Episode 23
Video: C in 100 Seconds: Structs | Episode 23 by Taught by Celeste AI - AI Coding Coach
Watch full page →Structs in C — Group Your Data
C in 100 Seconds, Episode 23
Variables are great for single values. But what happens when you need to represent something with multiple properties — like a student with a name, an age, and a GPA? That is where structs come in.
What Is a Struct?
A struct is a custom type that groups different types together under one name. Instead of managing three separate variables, you get one unit.
typedef struct {
char name[50];
int age;
float gpa;
} Student;
typedef lets you use Student directly as a type name. Without it, you would have to write struct Student every time.
Creating and Initializing
Initialize with curly braces — values go in the same order as the members were defined:
Student s = {"Alice", 20, 3.85};
Alice for name, 20 for age, 3.85 for GPA. One line, one variable, all three values.
Accessing Members
Use the dot operator to read any field:
printf("Name: %s\n", s.name); // Alice
printf("Age: %d\n", s.age); // 20
printf("GPA: %.2f\n", s.gpa); // 3.85
Modifying Members
Structs are mutable. Assign to any field to change it:
s.age = 21;
printf("Updated age: %d\n", s.age); // 21
The other fields stay the same. You are only changing what you target.
Why Structs Matter
Structs are the building block of organized data in C:
- Data modeling — represent real-world objects (students, points, records)
- Function parameters — pass a single struct instead of five separate arguments
- Arrays of structs — a list of students, a deck of cards, a table of records
- Pointer to struct — the arrow operator
->unlocks dynamic data structures
Without structs, C programs would be a mess of unrelated variables. With them, your data has shape.
Full Code
#include <stdio.h>
typedef struct {
char name[50];
int age;
float gpa;
} Student;
int main() {
Student s = {"Alice", 20, 3.85};
printf("Name: %s\n", s.name);
printf("Age: %d\n", s.age);
printf("GPA: %.2f\n", s.gpa);
s.age = 21;
printf("\nUpdated age: %d\n", s.age);
return 0;
}
Compile and run:
gcc structs.c -o structs
./structs
Next episode: Structs and Pointers — the arrow operator and heap-allocated structs.
Student code: github.com/GoCelesteAI/c-in-100-seconds/episode23