Following topics will cover in this article-
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.
1 |
typedef datatype alias_name; |
Example:
typedef unsigned int uint32_t;
Now, uint32_t is the synonym of unsigned int.
In C language, we can use typedef with following to reduce the complexity of declaration and explain the better meaning of your variable.
We can typedef existing primitive data type to an alias name that will define the real meaning of variable in the program.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
#include<stdio.h> #include<stdlib.h> typedef unsigned int meter_t; // you can avoid '_t' for simple notation int main() { meter_t rope_lenght=4000; //== unsigned int rope_lenght /*In above declaration it is more clear to others the rope_lenght is in 'meter'*/ printf("rope lenght in meter =%d\n",rope_lenght); } |
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
#include<stdio.h> /*structure without typedef*/ struct birthday { unsigned int date; char month[10]; unsigned int year; }; int main() { struct birthday bday={10,"oct",1989}; printf("My Birthday is=%u/%s/%u\n",bday.date,bday.month,bday.year); return 0; } |
In Above code, we can use typedef to simplify the structure declaration as shown below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
#include<stdio.h> typedef struct birthday { unsigned int date; char month[10]; unsigned int year; }myBday; int main() { //struct birthday bday={10,"oct",1989}; myBday bday={10 ,"oct" ,1989}; //myBday is synonym of struct birthday printf("My Birthday is=%u/%s/%u\n",bday.date,bday.month,bday.year); return 0; } |
we can also typedef above structure in following ways-
1 2 3 4 5 6 7 8 9 10 11 12 |
/*1.structure with typedef */ struct birthday { unsigned int date; char month[10]; unsigned int year; }; typedef struct birthday myBday;//mybday is the synonym of struct birthday |
1 2 3 4 5 6 7 8 9 10 |
/*2.structure with typedef*/ typedef struct { unsigned int date; char month[10]; unsigned int year; }birthday; |
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’.
1 2 3 4 5 6 7 8 9 10 11 |
/*union with typedef*/ typedef union { unsigned int date; char month[10]; unsigned int year; }birthday; |
typedef use with the array:
typedef is also used to simplify the array definition.
1 2 3 4 5 6 7 8 9 |
int marks[6]={20,30,40,50,80}; /*here int marks[6] is the array of 6 integer values.we can give an alias name to this array */ typedef int subject[6]; /*subject is the synonym for an integer array which has 6 elements so now can we simplify the array definition as follows.*/ subject marks={10,20,30,40,50,60}; |
typedef use with the pointer:
1 |
unsigned int *lenght; //length is a pointer to an integer |
we can give an alias name to this integer pointer like this-
1 |
typedef unsigned int * puint32_t; |
Now punit32_t is the synonym of unsigned int *
1 |
ptrLenght lenght;// ==unsigned int *lenght |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
#include <stdio.h> #include<string.h> #include<stdlib.h> struct birthday { unsigned int date; char month[10]; unsigned int year; }; int main(void) { typedef struct birthday *calender; /*calender is alias name for pointer struct birthday*/ calender pbday; pbday=(calender)malloc(sizeof(struct birthday)); pbday->date=10; strcpy(pbday->month,"Oct"); pbday->year=2017; printf("My Birthday is=%u/%s/%u\n",pbday->date,pbday->month,pbday->year); return 0; } |
1 |
My Birthday is=10/Oct/2017 |
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:
1 2 3 4 5 6 7 8 9 10 |
int maths(float arg1, int arg2) { return arg2; } int main_func(int (*call_this)(float, int)) { int output = call_this(5.5, 7); return output; } int final_result = main_func(&maths); |
This code can be rewritten with a typedef as follows:
1 2 3 4 5 6 7 8 9 10 11 12 |
typedef int (*maths)(float, int); int maths(float arg1, int arg2) { return arg2; } int main_func(maths call_this) { int output = call_this(5.5, 7); return output; } int final_result = main_func(&maths); |
In second block MathFunc is used as a synonym of such function which gives integer output and takes two input argument float and integer.
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