C Language

7 application of typedef in C language

The C language provides a very important keyword typedef for defining a new name to existing types. The typedef is the compiler directive mainly use with user-define data types (structure, union or enum) to reduce their complexity and increase the code readability and portability.

Syntax:

Let’s take an example,

Now UnsignedInt is a new type and using it, we can create a variable of unsigned int.

For example,
UnsignedInt Mydata;
In above example, Mydata is variable of unsigned int.

Note:  A typedef creates synonyms or a new name for existing types it does not create new types. 

Important: We have prepared a quiz on typedef mention below



There are two reasons to use the typedef in your program

Code Portability

It is too hard to modify complex expression everywhere in the source file, using the typedef we need to modify only a single line. That means it increase the portability of the code.

Let’s take an example,

Suppose here is an array of function pointer in your program and it is using in 10 places in the code.

 

In future due to any circumstance, might be you need to modify the parameter list of the function pointer in your program, then it will be too hard to change function pointer everywhere in the code.

We can reduce this complexity using the typedef, we need change only a single line statement for any future changes.

Code Readability

A typedef is increased the code readability to give the new name to a complex declaration.

Let’s take an example
Suppose here is a structure, which contains the information of a student.

Whenever in the program we need declare a variable of the above structure than every time we have to write the struct keyword with the tag name.

 

But if we are used the typedef with above structure then there are no need to write struct keyword at the time of variable declaration.

Now sStudentInformation is new types and whenever in the program we need the above structure variable then no need to write the struct.




Scope of typedef in C

The typedef has the advantage that it obeys the scope rules. That means you can use the same name for the different types in different scope. It can have file scope or block scope in which declare.

For the better understanding, I am taking two examples to describe how to typedef follow the scope rule in c.

Example code 1:

OutPut:

Explanation of the code

In the above code, I am giving the same name (TypeName ) to the two different types in different scope. This code is working fine because in the main () TypeName treat like as char * but globally it treats like the integer.

Example code 2:

OutPut:

Explanation of the code

In the above code, both types have the same name and they declare in the same scope. So here we will get the compiler error when try to compile the above code.

Application of typedef in C

In C language typedef keyword is used in many places. Generally, it is used with user define data types like structure, class, union or enum. The typedef gives the symbolic name to user define data types to reduce the complexity.

Use of typedef with variable

In a program, typedef is used to define the specific meaning of the variable.

Let’s take an example,

Using the typedef we can make above declaration more readable and portable.

In the above example distance is more readable in comparison of unsigned int.



Use of typedef with pointers

We can also use typedef with pointers to make the declaration more readable.

for example,

Let’s take an example for more understanding,

In below example distance and intPtr are the new name of int and int *.

OutPut:

Use of  typedef with structure

When used typedef with structure then no need to write struct every time at the variable declaration that means typedef save extra keystroke and make the code cleaner and readable.

For example,

Here, I am declaring a structure to store the student’s information. Whenever we create a variable of above structure then we have to use struct keyword (may be omitted in c++).

Using the typedef we can remove the extra keystroke struct at the time of variable declaration.

Now we can declare the variable of above structure without using the struct keyword.

We can also combine the typedef and structure together at the time of declaration.

There is no need of structure tag with a typedef that means we can also remove the structure tag at the time of declaration.

But it is my recommendation to use the structure tag at the time of structure declaration because it will create the problem when structure tries to reference itself.

Let’s take an example,

If we compile the below code then we will get the compiler error.

But if we used structure tag with above code then it will work fine.




Use of typedef with structure pointer

We can also use the typedef with structure pointers to reduce the complexity.

Let’s take an example,

We can create the pointer to the above structure.

using the typedef we can make it easier and readable.

Eg,

We can also combine the typedef with the structure at the time of declaration and create more than one types like one for the structure pointer and another for the structure variable.

Here,




Use of typedef with arrays

A typedef simplifies the use of the array in c language. Sometimes in the application, we need to pass the array in the function. Everything looking fine till the 1D array but when need to pass the 2D array in function then the complexity of the code is increased.

For the better understanding, I am taking here a function which takes an array as arguments.

OutPut:

Explanation:

In the above example, InitBrickPrice is the function which takes the pointer to the array as arguments and initializes the passed array with defined price.

Using the typedef we can make a declaration of the InitBrickPrice function easy.

OutPut:

 

Note: In above example, Brick_Price *paPrice is similar to int (*paPrice) [3];

We can also use typedef with 2D array,

 

Use of typedef with the function pointer

Using typedef, we can make the declaration of function pointer easy and readable. The typedef is very helpful when we create an array of the function pointer or a function returns a function pointer.

For example,
Here, I am creating a complex expression for the declaration of the array of function pointers. Using the typedef, we can make it easier.

Now, apfArithmatics is a new type and it is able to store the address of three functions.

In below example, AddTwoNumber, SubTwoNumber, and MulTwoNumber is a function which takes two integers as the argument and returns an integer.

 

Sometimes in a project, we need to type cast an address using the function pointer. We can do this task easier through the typedef.

For example,

 

using typedef

Used of typedef with enum

An enum is user define data types like as the structure and union. In the “c” language using the typedef, we can make the declaration of enum readable and portable like the structure.

Here I am creating some list of errors using the enumeration and define types using the typedef.

Now using the eErrorList we can define a variable of above enumeration type.

For example,

eErrorList  ePrinterDevice;

QUIZ on typedef




4 Comments

  1. Raghavendra

    It is good sight to learn C language.

  2. Subbu

    Hi Amlendra,

    Very nice article.

    I have one query related to below example that you have mentioned above. ??

    unsigned int KiloMeter;
    unsigned int Meter;
    unsigned int Miles;

    void SpeedCalculator(void)
    {
    int KiloMeter = 100; (Why here int need to use, in point of view its not needed.)
    //calculate speed in km/hr
    }

    int KiloMeter = 100; (Why here int need to use, in point of view its not needed.)

    Please correct me if i am wrong.

Leave a Reply

Theme by Anders Norén