C in 100 Seconds: Typedef | Episode 26
Video: C in 100 Seconds: Typedef — Custom Type Names | Episode 26 by Taught by Celeste AI - AI Coding Coach
Watch full page →Typedef in C — Custom Type Names for Cleaner Code
C in 100 Seconds, Episode 26
C types can get verbose. typedef lets you give any type a shorter, more meaningful name.
Simple Typedefs
typedef int Score;
typedef char* String;
Score is just int. String is just char star. The compiler treats them identically, but your code reads better.
Struct Typedefs
Without typedef you write struct Student everywhere. With it, just Student:
typedef struct {
String name;
Score grade;
} Student;
Student alice = {"Alice", 88};
Notice we used our own typedefs inside the struct — String and Score instead of char star and int.
Function Pointer Typedefs
This is where typedef really shines. Function pointer syntax is notoriously ugly:
int (*op)(int, int); // hard to read
With typedef:
typedef int (*MathOp)(int, int);
MathOp op = add; // clean
op(3, 4); // call it like a function
Point it at add, get seven. Point it at mul, get twelve. Same variable, different behavior.
Full Code
#include <stdio.h>
typedef int Score;
typedef char* String;
typedef struct {
String name;
Score grade;
} Student;
typedef int (*MathOp)(int, int);
int add(int a, int b) { return a + b; }
int mul(int a, int b) { return a * b; }
int main() {
Score s = 95;
String msg = "Hello";
printf("Score: %d\n", s);
printf("String: %s\n", msg);
Student alice = {"Alice", 88};
printf("Student: %s, %d\n", alice.name, alice.grade);
MathOp op = add;
printf("add(3,4) = %d\n", op(3, 4));
op = mul;
printf("mul(3,4) = %d\n", op(3, 4));
return 0;
}
Next episode: Function Pointers — callbacks, dispatch tables, and strategy patterns.
Student code: github.com/GoCelesteAI/c-in-100-seconds/episode26