typedef

Table of contents
  1. typedef
    1. Using typedef with a struct
    2. Using typedef with an enum
    3. A complete example
    4. The _t suffix
    5. Do not overuse typedef

The typedef keyword creates a shorter name for an existing type.

The basic syntax is:

typedef existing_type new_name;

For example:

typedef unsigned int uint;

We can then write:

uint count = 10;

instead of:

unsigned int count = 10;

This does not create a completely new type. uint is simply another name for unsigned int.

You will usually see typedef used with structs and enums, where it saves us from repeatedly writing struct or enum.

Using typedef with a struct

Without typedef, we include the struct keyword whenever we create a variable:

struct point {
    int x;
    int y;
};

struct point position = {
    .x = 10,
    .y = 20
};

We can define a shorter type name using typedef:

typedef struct {
    int x;
    int y;
} point_t;

We can then create variables using point_t:

point_t position = {
    .x = 10,
    .y = 20
};

This is a common pattern in C:

typedef struct {
    int sensor_id;
    float value;
} sensor_reading_t;

The name after the closing brace is the new type name:

sensor_reading_t reading;

Using typedef with an enum

Without typedef, an enum variable is declared like this:

enum direction {
    DIRECTION_UP,
    DIRECTION_DOWN,
    DIRECTION_LEFT,
    DIRECTION_RIGHT
};

enum direction player_direction = DIRECTION_UP;

Using typedef, we can create a shorter type name:

typedef enum {
    DIRECTION_UP,
    DIRECTION_DOWN,
    DIRECTION_LEFT,
    DIRECTION_RIGHT
} direction_t;

We can then write:

direction_t player_direction = DIRECTION_UP;

This style is commonly used for program states:

typedef enum {
    STATE_IDLE,
    STATE_RUNNING,
    STATE_ERROR
} state_t;

A complete example

This example combines a typedef struct and a typedef enum:

#include <stdio.h>

typedef enum {
    DIRECTION_UP,
    DIRECTION_DOWN,
    DIRECTION_LEFT,
    DIRECTION_RIGHT
} direction_t;

typedef struct {
    int x;
    int y;
    direction_t direction;
} player_t;

int main(void)
{
    player_t player = {
        .x = 10,
        .y = 20,
        .direction = DIRECTION_RIGHT
    };

    printf("Player position: (%d, %d)\n", player.x, player.y);

    return 0;
}

The type names are:

direction_t
player_t

The enum values are:

DIRECTION_UP
DIRECTION_DOWN
DIRECTION_LEFT
DIRECTION_RIGHT

The _t suffix

A common convention is to end typedef names with _t:

point_t
direction_t
player_t

This makes it easier to recognise that the name represents a type.

We use this convention in some of the module libraries, although C does not require it.

Do not overuse typedef

A typedef should make the code easier to read.

This is useful:

typedef struct {
    int x;
    int y;
} point_t;

This is probably not:

typedef int number;

The name number does not tell us anything more than int.

For now, the main thing to remember is that typedef gives an existing type another name. You will most often use it to create shorter names for structs and enums.


This site uses Just the Docs, a documentation theme for Jekyll.