typedef in C

In this class, we will try to understand typedef in C.

typedef in C

We have seen how to declare a structure tag in our previous class.

typedef in C

The image is the use of a structure tag and the declaration of variables for those structures.

Declaring Structure Tag typedef in C
Declaring Structure Tag With Variables

In the above declaration, we use the keyword struct for all the variable declarations.

In C, we have a provision to define the data types, which the typedef can do.

The typedef is used to create an additional name for the existing data type but does not create a new type.

For example, we want to create a new definition for int data type as an integer.

The image below shows how to create the type definitions.

typedef in C Example
Example

We can now use Integer for our variable declaration instead of int, as shown below.

typedef in C Example on int datatype
Example on int datatype

The use of typedef is frequently used for structures.

The declaration of the student structure with typedef is as shown below.

Structure Definition
Structure Definition

Now in the variable declaration, we can avoid the use of struct student as shown below.

Structure with structure variables
Structure with structure variables