• admin@embedclogic.com

typedef in C

Agenda:

Following topics will cover in this article-

  1. What is Typedef?
  2. How to define it?
  3. How to use in your program?
  4. Conclusion
1. What is typedef?

typedef is used to give an alias name to another data type. Normally we use the standard name of data type like int, unsigned int, char, struct etc. to represent the feature of variable and function in the program.

         typedef replaced standard data types with an alias name whatever you want(prefer name according to variable function is best practice).It also reduces the complexity of the program, we will see in the examples.

2. How to define ‘typedef’?
Syntax :  

Example:

typedef unsigned int uint32_t;

Now, uint32_t is the synonym of unsigned int.

2. How to use in C code?

In C language, we can use typedef with following to reduce the complexity of declaration and explain the better meaning of your variable.

  • typedef use with integer
  • typedef use in the structure
  • typedef use with union
  • typedef use with the array
  • typedef use with the pointer
  • typedef use with the function pointer
typedef use with integer:

  We can typedef existing primitive data type to an alias name that will define the real meaning of variable in the program.

In above example rather than using unsigned int we can use alias name meter_t because this alias name tells much more about the variable rope_lenght.

typedef use in structure:

use of typedef may ease the declaration of structure, union, and complex pointer.for example:

In Above code, we can use typedef to simplify the structure declaration as shown below.

we can also typedef above structure in following ways-

or

typedef use with the union:

typedef use in the union is same as we have seen in the structure except keyword used for the union is ‘union’ instead of ‘struct’.

typedef use with the array:

typedef is also used to simplify the array definition.

typedef use with the pointer:

  • typedef for a pointer to an integer-

we can give an alias name to this integer pointer like this-

Now punit32_t is the synonym of unsigned int *

  • typedef for a pointer to structure-

typedef use with the function pointer:

We can give an alias name to a function pointer.

Consider the following code, which does not use a typedef:

This code can be rewritten with a typedef as follows:

In second block MathFunc is used as a synonym of such function which gives integer output and takes two input argument float and integer.

Conclusion:

typedef is not compulsory to use. Basically, it is used to reduce the complexity of declaration as well as it gives you the freedom to choose the name of your interest of data type.You should choose an alias name for the type which can explain much more about your variable working. feel free to take any name of your new data type name.

Although I have tried to cover most of the part related to this topic your suggestion and comment are always welcome to improve this article. I also request you to send me if you find any error in this article .thank you so much for reading 🙂

 

reference: https://en.wikipedia.org/wiki/Typedef

Subscribe and stay updated with our latest articles.

You have successfully subscribed to the newsletter

There was an error while trying to send your request. Please try again.

Embedclogic will use the information you provide on this form to be in touch with you and to provide updates and marketing.