C typedef

In this tutorial you will learn about the C typedef and its application with practical example.

In C programming language typedef ( type definition ) is a keyword that allows us to create an alias for existing data types. Once, we have created a type definition, then we can declare a variable using the alias we created. This alias is equivalent to the data type for which it is created. Remember that, in addition to the type definition or alias we are always free to use the original keyword to define variables of any data type.

Syntax:

It is easy to create the alias, put the typedef keyword followed by the existing type name and the new name for that type.

Let’s take look at the example below:

We have created the alias for integer data type as “my_type”, now my_type behaves the same as an integer.

Advantages of C Typedef

  • It makes the program more portable.
  • typedef makes complex declarations easier to understand.

typedef with a struct

Take a look at below structure declaration

As we can see we have to include keyword struct every time you declare a new variable, but if we use typedef then the declaration will be as easy as below

this way c typedef makes your declaration simpler.

C Typedef with Union

typedef with enum

 

 

In this tutorial we have learn about the C typedef and its application with practical example. I hope you will like this tutorial.