Hello readers, welcome to our blog. In the last article, you had learnt about the Union In C Programming Language. I hope you all understood how to use a union in c, how to define union and how to access union member functions but in today’s article, you will learn how to use a c typedef keyword. So let’s start.
Introduction to C typedef Keyword
Typedef is a keyword in C language. This is used to give a new name to the current name of a data type. With this keyword, you can give new types of data types both built-in and user-defined.
This keyword is used mainly with user-defined data types such as structure etc. When the name of the data types in the program is complex, then this keyword can define its simple name. Using this keyword, the code decreases and the readability of the program increases.
For example, look at the given structure below.
You can create variables of the structure above here.
struct Employee e1;
The struct Employee has also been announced to create e1 in the declaration given above. Whenever you create a variable of structure, you will do the same way.
But using the c typedef keyword, you can make this declaration short and easy word. By doing so you can use the same word every time.
The syntax of C typedef Keyword
typedef-keyword data-type-name new-name;
As you can see in the syntax given above, the first typedef is declared. Then the name of that type is declared, which you want to change. At last, the new name is written which you want to use.
Example of C typedef Keyword
typedef struct Employee emp;emp e1;e1.Id=101;
e1.Salary=10000;printf(“Employee Id is %d and salary is %d\n”,e1.Id,e1.Salary);
In the example given above, the struct Employee declaration by typedef has been converted to emp. This example produces the below given output.
Employee Id is 101 and salary is 10000