typedef in C
What is typedef?
Some type names in C are long and repetitive — unsigned long int, struct Employee, or a tangled function-pointer type. typedef lets you give any existing type a short, readable alias, so you can use the short name everywhere instead.
Important: typedef does not create a new type. It only creates a nickname for a type that already exists. The variable behaves exactly the same — it is just easier to write and read.
A basic alias
#include <stdio.h>
typedef unsigned int uint; // uint now means unsigned int
int main() {
uint age = 25;
printf("Age = %u\n", age);
return 0;
}Age = 25
Here uint is simply a friendlier name for unsigned int. Both mean the same thing to the compiler.
typedef with struct
This is the most common use. Normally you repeat the struct keyword every time; typedef removes it.
#include <stdio.h>
typedef struct {
char name[20];
int age;
} Person; // Person is the alias
int main() {
Person p = {"Aman", 22};
printf("%s is %d\n", p.name, p.age);
return 0;
}Aman is 22
Thanks to typedef, you write Person p; instead of struct Person p; — cleaner and closer to how other languages name types. (New to structures? See structure vs union.)
typedef for pointers and function pointers
typedef really shines when a type is hard to read. Function pointers are the classic example.
#include <stdio.h>
typedef int (*Operation)(int, int); // alias for the messy type
int add(int a, int b) { return a + b; }
int main() {
Operation op = add; // clean and readable
printf("Sum = %d\n", op(5, 6));
return 0;
}Sum = 11
Without the typedef you would have to write the full int (*op)(int, int) every time. (See function pointers for the raw syntax.)
typedef vs #define
Both can name things, but they are not the same. #define is blind text replacement; typedef understands types.
| Point | typedef | #define |
|---|---|---|
| Handled by | Compiler | Preprocessor |
| Understands types | Yes | No (plain text) |
| Works with pointers safely | Yes | Often buggy |
| Scope rules | Follows normal scope | Text everywhere below it |
#define PTR int* then PTR a, b; makes only a a pointer. With typedef int* PTR;, both a and b are pointers. This is why typedef is safer for types.
Common mistakes
- Thinking typedef creates a new type — it only makes an alias.
- Using
#definefor pointer types and getting surprising declarations. - Putting the alias name in the wrong place (it goes at the end of the typedef).
- Overusing typedef for simple types, which can hide what a variable really is.
Define a typedef struct named Book with a title and a price, create two books, and print them. Then rewrite one declaration the long way (struct ... ) to prove both forms are identical.
Summary
typedefgives an existing type a short, readable alias — no new type is created.- With structs it removes the repeated
structkeyword (Person p;). - It makes pointer and function-pointer types far easier to read.
- Prefer
typedefover#definefor naming types — it is type-aware and safer.
Frequently Asked Questions
What is typedef in C?
typedef creates a new, shorter name (an alias) for an existing type. It does not create a new type — it just gives an existing one a more readable label. For example typedef unsigned long ulong; lets you write ulong instead of unsigned long.What is the difference between typedef and #define?
typedef is handled by the compiler and understands real types, so it works correctly even with pointers and complex declarations. #define is a plain text replacement done by the preprocessor with no type awareness, which can behave unexpectedly. For naming types, typedef is the safer choice.Why is typedef used with struct in C?
struct Point p; every time. With typedef struct { ... } Point; you can just write Point p;. It removes the repeated struct keyword and makes the code cleaner and easier to read.Does typedef create a new data type?
typedef only creates an alias for an existing type — the underlying type is unchanged. A variable declared with the alias is exactly the same type as one declared the long way, so they are fully interchangeable.Can typedef make function pointers easier to read?
typedef int (*Operation)(int, int); lets you then declare variables simply as Operation op; instead of repeating the full messy syntax each time.