TExcelTAD
Home › C › C typedef

C typedef

typedef gives an existing type a new name (an alias). It does not create a new type — the compiler treats the alias and the original as the same thing — but a good name can make code shorter, clearer and easier to change.

Basic syntax

Write typedef, then a normal declaration in which the "variable name" becomes the new type name:

c
typedef existing_type NewName;
c · try it yourself
#include <stdio.h>

typedef unsigned long long u64;
typedef double Celsius;

int main(void) {
    u64 big = 18000000000000000000ULL;
    Celsius today = 21.5;
    Celsius tomorrow = today + 2.0;      // still just a double

    printf("big = %llu\n", big);
    printf("today %.1f C, tomorrow %.1f C\n", today, tomorrow);
    return 0;
}
big = 18000000000000000000
today 21.5 C, tomorrow 23.5 C

Celsius tells the reader what the number means, even though the compiler sees a plain double.

typedef with structures

The most common use of typedef is to avoid writing struct every time you use a structure type:

c · try it yourself
#include <stdio.h>

typedef struct {
    double x;
    double y;
} Point;

typedef struct {
    Point top_left;
    Point bottom_right;
} Rect;

double rect_area(Rect r) {
    double w = r.bottom_right.x - r.top_left.x;
    double h = r.bottom_right.y - r.top_left.y;
    return w * h;
}

int main(void) {
    Point a = {1.0, 2.0};
    Rect r = {a, {6.0, 5.0}};
    printf("Rectangle from (%.0f,%.0f) to (%.0f,%.0f), area %.1f\n",
           r.top_left.x, r.top_left.y, r.bottom_right.x, r.bottom_right.y, rect_area(r));
    return 0;
}
Rectangle from (1,2) to (6,5), area 15.0

Compare Rect r with struct Rect r — shorter, and it reads like a built-in type.

Self-referencing structs need a tag

An anonymous typedef struct { ... } Name; cannot refer to itself, because Name does not exist yet inside the braces. Give the struct a tag as well and use struct Tag * inside:

c · try it yourself
#include <stdio.h>

typedef struct Node {
    int value;
    struct Node *next;     // the typedef name 'Node' is not available yet here
} Node;

int main(void) {
    Node third = {30, NULL};
    Node second = {20, &third};
    Node first = {10, &second};

    for (const Node *n = &first; n != NULL; n = n->next) {
        printf("%d -> ", n->value);
    }
    printf("NULL\n");
    return 0;
}
10 -> 20 -> 30 -> NULL

Using the same name for the tag and the typedef (struct Node / Node) is legal and common.

typedef for arrays and function pointers

typedef really earns its keep with complicated declarations. Function pointer types in particular are hard to read without it:

c · try it yourself
#include <stdio.h>

typedef int Vector3[3];                 // Vector3 is "array of 3 ints"
typedef int (*BinaryOp)(int, int);      // BinaryOp is "pointer to function (int,int) -> int"

int add(int a, int b) { return a + b; }
int mul(int a, int b) { return a * b; }

int fold(const int *values, int n, BinaryOp op, int start) {
    int acc = start;
    for (int i = 0; i < n; i++) {
        acc = op(acc, values[i]);
    }
    return acc;
}

int main(void) {
    Vector3 v = {2, 3, 4};
    printf("sum     = %d\n", fold(v, 3, add, 0));
    printf("product = %d\n", fold(v, 3, mul, 1));

    BinaryOp ops[] = {add, mul};
    printf("ops[1](6, 7) = %d\n", ops[1](6, 7));
    return 0;
}
sum     = 9
product = 24
ops[1](6, 7) = 42

Without the typedef, the parameter would be written int (*op)(int, int) — and an array of such pointers int (*ops[])(int, int).

Typedefs you already know

The standard library is full of typedefs that hide platform details:

Type Defined in Meaning
size_t <stddef.h>, <stdio.h>… unsigned type for sizes and counts
ptrdiff_t <stddef.h> signed difference between two pointers
int32_t, uint8_t, … <stdint.h> integers of an exact width
FILE <stdio.h> an open file stream
bool <stdbool.h> macro for _Bool (not a typedef, but the same idea)

Because size_t is a typedef, the same code works whether sizes are 32 or 64 bits on a given platform.

When (not) to use typedef

Good use Questionable use
shortening struct types used everywhere hiding that something is a pointer: typedef char *String;
naming function pointer types renaming basic types without adding meaning: typedef int Integer;
exact-width or platform-dependent types typedefs that make the reader look up what a type really is for every variable

Tip: Hiding a pointer behind a typedef (typedef struct Node *NodePtr;) is controversial: readers can no longer see that NodePtr p must be checked for NULL or freed. Many style guides (including the Linux kernel's) avoid it.

Test yourself

What does typedef float Meters; do?

  1. Creates a new incompatible type
  2. Gives float the alias Meters
  3. Declares a variable named Meters
  4. Converts floats to meters

After typedef struct { int x, y; } Point;, how do you declare a variable?

  1. struct Point p;
  2. Point p;
  3. typedef Point p;
  4. struct p Point;

Why must a self-referencing struct have a tag (e.g. struct Node)?

  1. Typedef names are not visible until the declaration ends
  2. Tags make structs faster
  3. Pointers need tags
  4. It is not required