typedef
typedef gives an existing type a second name. It creates an alias, not a new type, so the compiler still treats the alias and the original as the same type everywhere.
Detailed Explanation
C's declaration syntax reads outward from the name, which makes some types genuinely hard to say out loud. void (*handlers[8])(int, char *) is an array of eight pointers to functions taking an int and a char *. Writing that in five places is how bugs get in. typedef exists so you can name the type once and then use the name.
The syntax is a lie that turns out to be helpful. Write the declaration you would write for a variable of that type, then put typedef in front of it. The thing that would have been the variable name becomes the type name.
void (*handler)(int, char *); /* a variable named handler */
typedef void (*Handler)(int, char *); /* a type named Handler */
The most important thing to understand is that the alias is not a distinct type. If you write typedef int UserId; and typedef int ItemCount;, the compiler will happily let you pass a UserId where an ItemCount is expected. C has no way to say "an integer, but a different one". If you want that kind of separation you wrap the value in a one-member struct, which is a real distinct type, and pay for it with more verbose code. Rust's newtype pattern is the same idea with better ergonomics.
The classic use is structs. Without a typedef, every declaration of a struct needs the struct keyword: struct Point p;. With typedef struct { double x, y; } Point; you write Point p;. The tradeoff is that an unnamed struct has no tag, so the type cannot refer to itself, which matters the moment you write a linked list. The fix is to give the struct a tag and a typedef:
typedef struct Node Node; /* Node usable before the body is defined */
struct Node { int value; Node *next; };
| Form | struct keyword needed | Self-referential | Forward-declarable |
|---|---|---|---|
struct Point { ... }; | yes | yes | yes |
typedef struct { ... } Point; | no | no | no |
typedef struct Point { ... } Point; | no | yes | yes |
Where typedef turns harmful is when it hides a pointer. If Widget is secretly struct widget *, then a reader of Widget w; has no idea an indirection is involved, no idea whether w owns anything, and no idea that const Widget w qualifies the pointer rather than the pointed-to object. That last one surprises almost everyone: typedef char *String; const String s; means char *const s — a constant pointer to modifiable characters. The qualifier applies to the alias as a whole, and the alias is the pointer.
The exception, and the one place hiding a pointer is good practice, is an opaque handle: a type whose body the caller is deliberately forbidden to see, such as FILE * behind fopen. There the point is that you cannot look inside.
On naming: _t is the convention for type names in POSIX (size_t, pthread_mutex_t). POSIX reserves the _t suffix for the implementation, so a name you invent may one day collide with a system header. For application code, prefer a project prefix such as cp_buffer or CpBuffer.
Code Examples
#include <stdio.h>
/* Tag plus alias: Node can refer to itself, and callers skip "struct". */
typedef struct Node Node;
struct Node {
int value;
Node *next; /* legal because the tag Node exists */
};
/* Naming a function pointer type turns an unreadable declaration into a word. */
typedef int (*Compare)(int a, int b);
static int ascending(int a, int b) { return a - b; }
/* Compare reads as a type; without the typedef this parameter would be
"int (*cmp)(int, int)" wedged between the other parameters. */
static int larger(int a, int b, Compare cmp)
{
return cmp(a, b) >= 0 ? a : b;
}
int main(void)
{
Node tail = { 2, NULL };
Node head = { 1, &tail };
printf("%d\n", larger(head.value, tail.value, ascending)); /* prints: 2 */
return 0;
}#include <stdio.h>
typedef char *String; /* hiding a pointer: usually a bad idea */
int main(void)
{
char text[] = "hello";
/* const applies to the alias, which IS the pointer.
This is "char *const", not "const char *". */
const String s = text;
s[0] = 'H'; /* allowed: the characters are not const */
/* s = NULL; rejected: the pointer itself is const */
printf("%s\n", s); /* prints: Hello */
return 0;
}Common Gotchas
`const` on a pointer typedef qualifies the pointer, not the target
`const String s` expands to `char *const s`, so the pointer is frozen and the characters are not. This is the single most common surprise with `typedef`, and it is why hiding a `*` inside an alias is discouraged outside of opaque handles.
A typedef alias is never a new type
two aliases for `int` are interchangeable, and the compiler will not warn when you swap a user ID for an item count. Use a one-member `struct` when you want the compiler to enforce the distinction.
An anonymous `typedef struct` cannot refer to itself
`typedef struct { Node *next; } Node;` does not compile, because the alias does not exist yet inside the braces. Declare a tag as well.
`_t` names are reserved by POSIX
`mytype_t` is legal today and may collide with a future system header. Prefix your project's types instead.