typedef in C gives a new name to an existing type: typedef unsigned long ulong_t; lets you write ulong_t wherever you would write unsigned long. It creates an alias only, not a new type, and it is most useful for structs, function pointers and platform-dependent integer types.
typedef syntax in C
A typedef declaration looks like an ordinary variable declaration with the keyword typedef in front. The name that would have been the variable becomes the alias:
int count; /* count is an int variable */
typedef int count_t; /* count_t is another name for int */
count_t items = 3; /* same as: int items = 3; */
This works for any declarator, including pointers and arrays. The reference at cppreference shows several aliases in one declaration:
typedef char char_t, *char_p, (*fp)(void);
/* char_t is char
char_p is char *
fp is a pointer to a function taking no arguments and returning char */
Technically typedef sits in the grammar where a storage-class specifier goes (the same slot as static and extern), but it does not affect storage or linkage. Because a declaration can have only one storage-class specifier, you cannot combine it with static or extern. Typedef names also share a namespace with ordinary identifiers such as variables, functions and enumerators, so a typedef name cannot be reused as a variable name in the same scope.
A typedef is an alias, not a new type
The alias and the original type are fully compatible. The compiler does not treat them as different types, so it will not catch mistakes like this:
typedef int meters_t;
typedef int seconds_t;
meters_t distance = 100;
seconds_t duration = distance; /* compiles without complaint */
If you want the compiler to reject that, wrap each value in its own struct, because two differently named struct types are not compatible. A typedef alone gives readability, not type safety.
typedef with struct
This is the most common use. Without a typedef, every use of a struct needs the struct keyword, because struct tags live in their own namespace:
struct point {
double x;
double y;
};
struct point origin = {0.0, 0.0};
With a typedef you can drop the keyword. There are two equivalent styles. The first names the struct and the alias separately:
typedef struct point point; /* alias in the ordinary namespace */
struct point {
double x;
double y;
};
point origin = {0.0, 0.0};
The second defines the struct and the alias in a single declaration, and may leave out the tag:
typedef struct {
double hi;
double lo;
} range;
range r = {1.0, 0.0};
Both forms are shown on the cppreference typedef page. The tag-less form is shorter, but it has one limit: a struct that points to itself needs a name to refer to inside its own body.
Self-referencing structs (linked lists)
Inside the braces of a one-step typedef the alias does not exist yet, so a node cannot use it for its next pointer. Declare the alias first, then use it:
typedef struct node node;
struct node {
int value;
node *next; /* same as: struct node *next; */
};
This is the pattern cppreference uses for its tnode example. If you write typedef struct { int value; node *next; } node; instead, the compiler reports that node is an unknown type name at the next member. The exact wording depends on the compiler.
typedef with function pointers
Function pointer declarations are hard to read, and a typedef makes them manageable. Compare a callback parameter written both ways:
/* Without typedef */
void for_each(int *items, size_t count, void (*action)(int));
/* With typedef */
typedef void (*action_fn)(int);
void for_each(int *items, size_t count, action_fn action);
In typedef void (*action_fn)(int); the alias name sits where a variable name would sit in void (*action)(int);. So action_fn means "pointer to a function that takes an int and returns nothing". Here is a full example you can compile:
#include <stddef.h>
#include <stdio.h>
typedef void (*action_fn)(int);
static void print_int(int value) { printf("%d\n", value); }
static void for_each(const int *items, size_t count, action_fn action)
{
for (size_t index = 0; index < count; index++) {
action(items[index]);
}
}
int main(void)
{
int numbers[] = {4, 8, 15};
for_each(numbers, 3, print_int);
return 0;
}
Example output:
4
8
15
The savings grow with the complexity of the declaration. cppreference gives a declaration for "an array of 5 pointers to functions returning pointers to arrays of 3 ints", which is hard to read: int (*(*callbacks[5])(void))[3];. With two typedefs it becomes:
typedef int arr_t[3]; /* array of 3 int */
typedef arr_t *(*fp)(void); /* pointer to function returning arr_t * */
fp callbacks[5];
typedef for portable integer types
Another reason to use typedef is to give a platform-dependent type a stable name. The C library does this itself. size_t is an implementation-defined unsigned integer type: it is the type of the result of sizeof, and it is declared in headers such as <stddef.h>, <stdio.h>, <stdlib.h> and <string.h>. On a 64-bit platform it is typically 64 bits wide, but the standard only guarantees at least 16 bits. Using size_t for sizes and array indexes avoids bugs where an unsigned int index overflows on large arrays.
The fixed-width names in <stdint.h> are typedefs too. uint8_t, int32_t and uint64_t stand for the unsigned or signed type with exactly that many bits and no padding bits. They are optional: an implementation defines one only if it has a matching type. The related int_least32_t and int_fast32_t families give the smallest and the fastest type with at least that width.
You can use the same idea in your own code. If a file format needs a 32-bit unsigned length, declare it once:
#include <stdint.h>
typedef uint32_t record_length_t;
If the underlying type ever needs to change, you edit one line instead of every declaration. For how sizes and allocation interact with these types, see malloc in C.
typedef and pointers: a common trap
A typedef that hides a pointer can surprise you. Because the alias is a single type, it behaves differently from a macro:
typedef char *string_t;
string_t first, second; /* both are char * */
#define STRING char *
STRING third, fourth; /* expands to: char * third, fourth;
third is char *, fourth is char */
This is the main practical difference between typedef and #define. A typedef is handled by the compiler as part of declaration syntax, while a macro is plain text replacement done before compilation.
Hiding the * inside an alias also has a cost for readers. With typedef struct node *node_ptr;, the declaration const node_ptr p; makes the pointer constant, not the node it points to, which is the opposite of what many readers expect. If you want the pointer visible, keep the asterisk in your declarations and alias only the struct. To go further with pointer syntax, see pointers in C.
Scope and where to put typedefs
A typedef follows normal scope rules. One declared inside a function is visible only in that block, and one declared at file scope is visible from that point to the end of the translation unit. Shared typedefs belong in a header, so every file that uses the type sees the same definition. Put them behind include guards, and keep them next to the declarations that need them.
One special case: a typedef for a variable-length array can only appear at block scope, and the array size is evaluated when control passes over the typedef, not when you later declare an object of that type. In the cppreference example, changing n after typedef int B[n]; does not change the size of arrays declared with B. Most code never needs this.
Naming conventions
C has no single rule. Common habits are a _t suffix (size_t, uint32_t), a capitalised name (Point), or the same name as the struct tag (typedef struct point point;). Pick one and apply it across the project.
When not to use typedef
- Aliasing a plain built-in for no reason.
typedef int integer;only makes readers look up whatintegermeans. - Hiding a pointer. As shown above, it makes
constand ownership harder to see. - Expecting type checking. Two aliases of
intare interchangeable. Use distinct structs if the compiler must keep them apart.
If it still fails to compile
If the compiler complains about an unknown type name, check that the typedef appears earlier in the file than its first use, and that the header declaring it is included. If a self-referencing struct fails, declare the alias with typedef struct name name; before the struct body. If you see a redefinition error, check whether the same typedef name is declared in two places with different types.
Frequently asked questions
- Does typedef create a new type in C?
- No. A typedef only creates a synonym for an existing type, so the alias and the original type are compatible. If you typedef int to a name like meters_t, the compiler will still let you pass it wherever an int is expected, and it will not warn about mixing it with other int-based aliases.
- What is the difference between typedef and #define in C?
- typedef is processed by the compiler and respects scope and C's declaration syntax. #define is text substitution by the preprocessor. With typedef char *string_t; both a and b in 'string_t a, b;' are pointers. With #define STRING char *, the same line expands to 'char * a, b;' and b is a plain char.
- Do I need a struct tag if I use typedef?
- Only if the struct refers to itself, as in a linked list node. Inside the braces the typedef name is not declared yet, so a self-referencing member must be written as 'struct node *next'. For structs that never point to themselves, 'typedef struct { ... } name;' with no tag is valid.
- Is uint32_t a typedef?
- Yes. The fixed-width names in stdint.h, such as int32_t and uint8_t, are typedef names for whichever built-in type has exactly that width on the platform. They are optional: an implementation defines one only if it has a matching type.