Skip to content

Module 7 · Structures, Unions, Enums and typedef — topic 4 of 4

typedef

typedef creates an alias for an existing type, making complex declarations readable and code easier to change. Learn typedef with structs, pointers, arrays and function pointers — and when not to use it.

Beginner 10 min read·Lesson 30 of 37 in C Programming

Learning objectives

  • Create type aliases with typedef
  • Simplify struct and function-pointer declarations
  • Recognise when typedef hides important information

Definition

A typedef declaration introduces a new name (an alias) for an existing type; it does not create a new distinct type.

Introduction

Writing struct Student everywhere is wordy, and a declaration like int (*cmp)(const void *, const void *) is hard to read. typedef gives such types short, meaningful names.

The standard library uses typedef extensively: size_t, FILE, uint32_t and time_t are all typedef names that hide platform-specific details behind a portable name.

Why it matters

Readable type names make large programs easier to maintain, and changing the underlying type in one place updates the whole program.

Function-pointer callbacks and opaque library handles are almost always declared with typedef.

Detailed explanation

typedef existing_type NewName; — the syntax is a normal declaration with typedef in front; the name being declared becomes the alias.

typedef struct { ... } Student; lets you write Student s;. To refer to the type inside itself (e.g. a linked-list node), give the struct a tag: typedef struct Node { int v; struct Node *next; } Node;.

typedef int (*Comparator)(const void *, const void *); names a function-pointer type, so parameters can be declared as Comparator cmp.

typedef aliases are interchangeable with the original type — typedef int Meters; typedef int Seconds; does not stop you adding meters to seconds. For real type safety wrap values in distinct structs.

Avoid typedefs that hide pointers (typedef struct Node *NodePtr;) in most code: they make it unclear that a variable is a pointer that may be null or must be freed. Opaque library handles are the accepted exception.

Syntax

C
typedef unsigned long long u64;
typedef struct { double x, y; } Vec2;
typedef struct Node { int value; struct Node *next; } Node;
typedef int (*BinOp)(int, int);
typedef char Name[32]; /* array type */
  • A typedef name follows normal scope rules.
  • By convention typedef names are PascalCase or end in _t (though _t names are reserved by POSIX).
  • typedef does not allocate memory or create a variable.

Code examples

Aliases for readability

C
#include <stdio.h>
typedef unsigned int uint;
typedef struct { double x, y; } Vec2;
Vec2 add(Vec2 a, Vec2 b) {
Vec2 r = {a.x + b.x, a.y + b.y};
return r;
}
int main(void) {
uint count = 3;
Vec2 p = {1.0, 2.0}, q = {0.5, -1.0};
Vec2 s = add(p, q);
printf("%u vectors, sum (%.1f, %.1f)\n", count, s.x, s.y);
return 0;
}
Output
3 vectors, sum (1.5, 1.0)

Self-referential struct with a tag

C
#include <stdio.h>
typedef struct Node {
int value;
struct Node *next;
} Node;
int main(void) {
Node c = {3, NULL}, b = {2, &c}, a = {1, &b};
for (const Node *p = &a; p != NULL; p = p->next)
printf("%d ", p->value);
printf("\n");
return 0;
}
Output
1 2 3

More examples

Function-pointer typedefs for a dispatch table

C
#include <stdio.h>
#include <string.h>
typedef double (*Converter)(double);
typedef struct {
const char *name;
Converter fn;
} Conversion;
double c_to_f(double c) { return c * 9 / 5 + 32; }
double km_to_mi(double k) { return k * 0.621371; }
double kg_to_lb(double k) { return k * 2.20462; }
static const Conversion TABLE[] = {
{"c2f", c_to_f},
{"km2mi", km_to_mi},
{"kg2lb", kg_to_lb},
};
Converter find(const char *name) {
for (size_t i = 0; i < sizeof TABLE / sizeof TABLE[0]; i++)
if (strcmp(TABLE[i].name, name) == 0) return TABLE[i].fn;
return NULL;
}
int main(void) {
const char *requests[] = {"c2f", "km2mi", "mph"};
for (int i = 0; i < 3; i++) {
Converter f = find(requests[i]);
if (f) printf("%-6s 100 -> %.2f\n", requests[i], f(100));
else printf("%-6s unknown conversion\n", requests[i]);
}
return 0;
}
Output
c2f    100 -> 212.00
km2mi  100 -> 62.14
mph    unknown conversion

Step-by-step explanation

  • Converter names “pointer to a function taking and returning double”.
  • Conversion pairs a name with a converter; TABLE is a constant lookup table.
  • find searches by name and returns a function pointer, or NULL if not found.
  • The caller checks for NULL before calling f(100).

Expected output

Running “Function-pointer typedefs for a dispatch table” prints exactly:

TextOutput
c2f 100 -> 212.00
km2mi 100 -> 62.14
mph unknown conversion

Practical applications

  • FILE, size_t, pthread_t and uint8_t are typedefs in standard and POSIX headers.
  • Embedded code defines typedef volatile uint32_t reg32_t; for hardware registers.
  • Plugin systems use typedef'd function pointers for callbacks.

Best practices

  • Use typedef for structs used throughout a program and for function-pointer types.
  • Don't hide pointers behind typedefs except for opaque handles.
  • Keep typedefs in the header that defines the type.

Common mistakes

Using the typedef name inside its own definition

Avoid

typedef struct {
int v;
Node *next; /* Node not yet declared */
} Node;

Prefer

typedef struct Node {
int v;
struct Node *next;
} Node;

The alias does not exist until the typedef ends. Use a struct tag for self-reference.

Expecting typedef to create a distinct type

Avoid

typedef int Celsius;
typedef int Fahrenheit;
Celsius c = 10; Fahrenheit f = c; /* no error */

Prefer

typedef struct { double value; } Celsius;
typedef struct { double value; } Fahrenheit;

Aliases are interchangeable. Distinct structs make mixing units a compile error.

Important notes

  • Names ending in _t are reserved by POSIX for system types; many projects avoid them for their own typedefs.
  • C11 allows repeating an identical typedef, which C99 did not.

Key takeaways

  • typedef creates an alias, not a new type.
  • Tag self-referential structs.
  • Typedefs make function-pointer declarations readable.

Try it yourself

  1. 1
    Rewrite the Employee example from the structures lesson using typedef struct.
  2. 2
    Create typedef int (*Predicate)(int); and write int count_if(const int *a, int n, Predicate p).

Finished reading? Mark it complete to update your dashboard.

Suggested practice

3 questions test this exact topic. Answers and explanations are revealed after you submit.

  • BeginnerWhat does `typedef unsigned int uint;` do?
  • ModerateWhy does this fail? `typedef struct { int v; Node *next; } Node;`
  • AdvancedGiven `typedef int *IntPtr;`, what is the type of b in `IntPtr a, b;`?
Practise this topic