Language
日本語
English

Caution

JavaScript is disabled in your browser.
This site uses JavaScript for features such as search.
For the best experience, please enable JavaScript before browsing this site.

  1. Home
  2. C Language Dictionary
  3. typedef

typedef

Since: C89(1989)

Creates an alias (alternative name) for an existing type. It is used to shorten long type names, abstract platform-dependent types, and make working with structs, unions, and enums more convenient.

Syntax

// Creates an alias for a basic type.
typedef existing_type new_type_name;

// Creates an alias for a struct (allowing you to omit the struct keyword).
typedef struct {
    type member;
} TypeName;

// Creates an alias for a function pointer type.
typedef return_type (*TypeName)(param_type, ...);

// Creates an alias for an array type.
typedef type TypeName[size];

Common uses of typedef

UseDescription
Shortening type namesShortens verbose names like unsigned long long int to something like uint64_t for easier writing.
Omitting structBy giving a struct declaration an alias, you can omit the struct keyword when declaring variables.
Improving portabilityWraps platform-dependent types under an alias, centralizing any necessary changes to a single location in the source code.
Readable function pointersGives a meaningful name to a function pointer type, making declarations easier to read.
Defining abstract typesHides internal implementation details from API users, so changes to the underlying type do not affect callers.

Sample code

sample_typedef.c
#include <stdio.h>
#include <stdint.h> // Defines types such as uint8_t and uint32_t.

// Creates aliases for basic types.
typedef unsigned char  byte_t;
typedef unsigned int   uint_t;
typedef long long      llong_t;

// Creates an alias for a struct (the struct keyword is no longer needed).
typedef struct {
    int   x;
    int   y;
} Point;

// Creates an alias for a function pointer type.
typedef int (*Comparator)(const void *, const void *);

// Creates an alias for an array type.
typedef char Name[64];

int compare_int(const void *a, const void *b) {
    return (*(int *)a - *(int *)b);
}

int main(void) {
    // Uses the typedef'd types.
    byte_t b = 255;
    uint_t n = 1000000u;
    printf("byte_t: %u, uint_t: %u\n", b, n);

    // Uses the Point struct without the struct keyword.
    Point p1 = {10, 20};
    Point p2 = {.x = 30, .y = 40};
    printf("p1: (%d, %d)\n", p1.x, p1.y); // Outputs "p1: (10, 20)".
    printf("p2: (%d, %d)\n", p2.x, p2.y); // Outputs "p2: (30, 40)".

    // Uses the function pointer type alias.
    Comparator cmp = compare_int;
    int a = 5, bb = 3;
    printf("Comparison result: %d\n", cmp(&a, &bb)); // Outputs a positive value.

    // Uses the array type alias.
    Name username = "Bulma";
    printf("Username: %s\n", username); // Outputs "Username: Bulma".

    // Fixed-width integer types from stdint.h (defined internally using typedef).
    uint8_t  u8  = 255;
    uint32_t u32 = 4000000000u;
    printf("uint8_t: %u\n", u8);
    printf("uint32_t: %u\n", u32);

    return 0;
}
gcc typedef.c -o typedef
./typedef
byte_t: 255, uint_t: 1000000
p1: (10, 20)
p2: (30, 40)
Comparison result: 2
Username: Bulma
uint8_t: 255
uint32_t: 4000000000

Notes

typedef does not create a new type — it only creates an alias for an existing one. The alias is fully compatible with the original type, so either can be used wherever the other is expected.

The C standard header <stdint.h> provides fixed-width integer types such as uint8_t, int32_t, and uint64_t, all of which are defined internally using typedef. For cross-platform code, it is recommended to use these fixed-width types instead of int or long, whose sizes are implementation-defined.

The most common use of typedef is in combination with structs, unions, and enums. See struct, union, and enum for details.

If you find any errors or copyright issues, please .