typedef: giving types friendly names
6 min lesson
- typedef creates a new, friendlier name for an existing type, so code reads more clearly.
- It matters because some types, especially structs and pointers, have long or awkward names that clutter the code.
- Think of typedef as a nickname: the type keeps its real name, but you can call it something shorter and friendlier.
- The syntax is: typedef existingType NewName; for example typedef unsigned int uint;.
- A very common use is giving a struct a single-word name: typedef struct Student { ... } Student;.
- After the typedef, you can declare variables with the new name: Student alice; instead of struct Student alice;.
- The computer treats the new name exactly like the old type; typedef adds no runtime cost.
- A realistic use: naming a struct for a point, a function pointer type for a callback, or a fixed-width integer for portability.
- A surprising detail: typedef does not create a new type, only an alias; the compiler still sees the original type underneath.
- The example program defines a typedef for a struct and for an unsigned int, then uses both new names in main.
- Use typedef to shorten long type names and make code easier to read.
- The classic use is giving a struct a single-word name.
- Remember that typedef is an alias, not a new type.
- Put typedefs in headers so all files share the same names.
- Small change, big readability win. Nicely done!
#include <stdio.h>
// Give the struct a single-word name: Point
typedef struct Point {
int x;
int y;
} Point;
// A shorter name for unsigned int
typedef unsigned int uint;
int main(void) {
// Declare a Point using the friendly name (no 'struct' keyword)
Point p;
// Set its members
p.x = 3;
p.y = 7;
// Print the coordinates
printf("Point: (%d, %d)\n", p.x, p.y);
// Use the uint alias for a counter
uint count = 5;
// Print the counter
printf("Count: %u\n", count);
return 0;
}What happens: Point: (3, 7) Count: 5
Breaking it down
Struct typedef
typedef struct Point { int x; int y; } Point;Defines the struct and gives it the alias Point in one step. After this, you can write Point p; without the struct keyword.
Integer typedef
typedef unsigned int uint;Creates the alias uint for unsigned int. It does not change how the type behaves; it only gives it a shorter name.
Using the alias
Point p;Declares a Point using the new name. The compiler treats it exactly like struct Point.
Format specifier
printf("%u", count);Prints an unsigned int. Using %d here would be technically wrong for unsigned values.
A helpful analogy
typedef is like a nickname. Your friend's full name might be 'Alexander', but everyone calls him 'Alex'. The person is the same; only the name used to refer to him is shorter and friendlier.
Nesting rules
- 1.typedef follows the same order as a normal declaration, with typedef in front.
- 2.The alias does not replace the original type; both names remain usable.
- 3.Typedefs for structs are usually placed in header files so every file shares them.
- 4.Do not confuse typedef with #define; typedef is understood by the compiler and respects scope.
Common mistakes
Forgetting the semicolon after typedef
Avoid
typedef unsigned int uintUse instead
typedef unsigned int uint;The typedef is a statement and needs a semicolon. Without it the compiler reports errors on the next line.
Confusing typedef with a variable declaration
Avoid
typedef int myInt = 5; // errorUse instead
typedef int myInt;
myInt x = 5;typedef creates a type alias, not a variable. You cannot initialize an alias; declare a variable of the alias afterward.
Using the alias before it is defined
Avoid
Point p;
typedef struct { int x; } Point;Use instead
typedef struct { int x; } Point;
Point p;The compiler must see the typedef before it sees the alias in use, or it reports an unknown type.
Quick tips
- •Use typedef for structs to drop the repeated struct keyword.
- •Name typedefs with a capital letter so they stand out from variables.
- •Use typedef for function pointer types to make callbacks readable.
- •Try writing Point q = {1, 2}; to see brace initialization with the alias.
- •Keep typedefs near the top of the file or in a header for easy reuse.