Skip to main content Link Menu Expand (external link) Document Search Copy Copied

typedef keyword

C

Keyword

Reference manual

@see Structures

The typedef keyword is used to create new names for existing data types. It essentially provides a way to define aliases for data types.

Syntax

Primitive types

In the following example we define Age as an alias for the int data type and we declare a variable using the alias. The only thing that changes here is the readability of the code.

typedef int Age;

int main(void)
{
    Age person_age = 32;
    return (0);
}

Complex types

In the following example we use typedef to define aliases for two structures making the declaration of struct instances easier to read.

When we use typedef with structures the name of the struct is optional as shown in the example bellow.

typedef struct 
{
    char first_name[50];
    char last_name[50];
} FullName;

typedef struct car
{
    char brand[20];
    int year;
} Car;

int main(void)
{
    FullName full_name = {"Erlich", "Bachman"};
    Car car = {"Toyota", 2022};
    return (0);
}