C typedef

Summary: in this tutorial, we will show you 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.

c typedefC 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 definition.

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

C typedef syntax

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

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

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

The scope of the new type depends on the position 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 your code, you can declare variable with the score type instead of unsigned int. Later on, 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 every place that you use unsigned int.

Another great example of using typedef for code portability is size_t. The size_t is return type of the sizeof operator that determines the size of a specific type based on the arithmetic capabilities of 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 is used to make your code easier to maintain and increase its readability. This readability can be achieved by simplifying 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.

Using C typedef with a struct

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

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. Hence using typedef helps you make the variable declaration simpler.

Using C typedef union

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:

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:

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

Using C typedef with function pointers

A function pointer is a special pointer that points to a function or hold address of a function. You can use typedef to give a function pointer a simpler name. Consider the following example:

We’ve defined a sorter function pointer as a new type name and then used it to declare quicksort and bubblesort function pointers.

In this tutorial, you have learned how to create new name for existing type using the C typedef keyword.