A structured lesson workspace with readable content, hands-on examples, and a clean path to completion.
typedef does not create a new type. It creates an alias for an existing type. typedef int Integer; means Integer and int are the same type, interchangeable everywhere. The compiler does not distinguish them. There is no type safety gained. What you gain is readability: a name that communicates intent rather than mechanical detail.
#include <stdio.h>#include <stdint.h>typedef unsigned char byte;typedef uint32_t user_id_t;typedef int (*compare_fn)(const void *, const void *);int main(void) {byte flags = 0xFF;user_id_t uid = 42;printf("flags: 0x%02X\n", flags);printf("uid: %u\n", uid);return 0;}
byte communicates that this variable holds a raw byte, not a character or a small integer. user_id_t communicates domain meaning. The _t suffix is a POSIX convention for typedef names. You will see it everywhere in system headers: size_t, pid_t, time_t. The compare_fn alias hides the grotesque syntax of a function pointer declaration behind a readable name.
In C, declaring a struct variable requires the struct keyword every time: struct Point p; typedef lets you drop it. This is idiomatic in most C codebases, though the Linux kernel famously avoids it for structs, preferring the explicit struct keyword as a signal that you are dealing with a composite type.
#include <stdio.h>typedef struct {double x;double y;} Point;typedef struct {Point start;Point end;} Line;int main(void) {Point p1 = {1.0, 2.0};Point p2 = {4.0, 6.0};Line segment = {p1, p2};printf("Segment from (%.1f, %.1f) to (%.1f, %.1f)\n",segment.start.x, segment.start.y,segment.end.x, segment.end.y);return 0;}
The argument against typedef for structs: it hides the fact that you are working with a struct, making it harder to grep for struct definitions and obscuring the type category. The argument for: it reduces noise, especially with nested structs and function pointers. In application code, typedef is standard. In kernel and systems code, you will see both styles. Read the project's existing code and match its convention.
typedef is a readability tool. It does not add safety. It does not prevent misuse. It makes declarations shorter and intent clearer. Use it for function pointer types, for domain-specific integer aliases, and for structs in application code. Next: enum, which gives names to integer constants and creates a poor man's algebraic type.
Finish the lesson once you have worked through the material. This awards ★ 20 XP.