C typedef

Summary: in this tutorial, you will learn how to create a new name for existing types using C typedef. The typedef keyword helps you simplify the complex declaration and makes your code more portable.

Introduction to the C typedef keyword

C language provides you with the typedef keyword that allows you to assign new names for existing types. Literally, typedef stand for type efinition.

It is important to notice that typedef just creates a new name for the existing type. It does not create a new type.

C typedef syntax

To create a new name for an existing type, you put the typedef keyword first, and the existing type name, and then the new name as follows:

typedef existing_type new_name;
Code language: C++ (cpp)

For example, if you want to create a new type name called score for unsinged integer, you just use the typedef as follows:

typedef unsigned int score;
Code language: C++ (cpp)

From now, you can declare variables with the new type, score. For example:

score high_score;
Code language: C++ (cpp)

The scope of the new type depends on the location where you define it. If the new type is defined inside a function, its scope is local to that function. The scope of the new type is global if the new type is defined globally.

C typedef usages

First, the typedef keyword is used to make your code more portable. Suppose you create a new type name called score for unsigned int . In the code, you can declare a variable with the score type instead of unsigned int.

Later, if you want a bigger integer to store scores, you just need to change the definition of the score type in one place instead of multiple places where you use the unsigned int.

Another good example of using typedef for code portability is size_t. The size_t is the return type of the sizeof operator that determines the size of a specific type based on the arithmetic capabilities of the target processor instead of memory capabilities.

As long as you include stddef.h library in your program, you will get the exact size of any object in your platform that implements C using the sizeof operator.

Second, typedef makes your code easier to maintain. This readability can be achieved by simplifying the declaration of complex types such as function pointer, struct, and enum.

C typedef examples

Let’s take some examples of using typedef with struct, union, enum and function pointer.

1) Using C typedef with a struct example

To define a complex number, you use a struct as follows:

struct complex { float real; float imaginary; }; struct complex a, b;
Code language: C++ (cpp)

Without using typedef, you have to include keyword struct every time you declare a new variable.

However, if you use the typedef to define the complex number, you can omit the struct keyword whenever you declare a new variable. Therefore, the typedef keyword makes the variable declaration simpler.

typedef struct { float real; float imaginary; } complex; complex a, b;
Code language: C++ (cpp)

2) Using C typedef union example

You can use typedef with union. Suppose your program allows users to use either username or email to login. You can define account struct with account_name union as follows:

typedef union { char *username; char *email; } account_name; typedef struct { account_name name; char *password; } account; account user1, user2;
Code language: C++ (cpp)

3) Using C typedef with enum

When combining with enum, typedef provides an additional abstraction layer for the enumeration type.

The following example demonstrates how to use typedef with an enum:

typedef enum { red, green, blue } RGB; RGB color;
Code language: C++ (cpp)

Without using typedef, you have to use enum keyword whenever you declare a new enum:

enum RGB { red, green, blue }; enum RGB color;
Code language: C++ (cpp)

4) Using C typedef with function pointers

A function pointer is a special pointer that points to a function or holds the address of a function. The typedef can assign a function pointer a simpler name. For example:

typedef int (*sorter)(void *a, size_t size); sorter quick_sort, bubble_sort;
Code language: C++ (cpp)

In this example, we define a sorter function pointer as a new type and then use it to declare the quick_sort and bubble_sort function pointers.

Summary

  • Use typedef keyword to assign an existing type a new name.
  • Use typedef keyword to make your code more concise and readable.
Was this tutorial helpful ?