Typedef
In addition to the simple data types, such as integer, float, char, etc., C allows the users to declare new data types called user defined data types. These data type can be declared with the help of a keyword called Typedef whose general form is given below,
typedef<type><new_type>;
Where typedef -> is a reserved word, <type> -> is the desired user defined data type. <new_type> -> is the desired used defined data type.
For Example, consider the following declaration,
typdef float abc;
In this declaration, an existing data type (float) has been redefined as abc. Now the user can use as a synonym for the type float as a new type as shown below: –
abc x, y;
In fact this declaration is equivalent to the following: –
float x, y;
It has a limitation that user will have to remember abc as a new type whereas its equivalent float type is already available.
Free Notes PDF
But this utility of typedef is used where the name of a particular type is very long.
e.g.
struct employee
{
char name[20];
int age;
float salary;
};
typedef struct employee emp; // New data type emp defined by the user
emp e1, e2, e3;
For Example, the declaration: –
typedef int age;
can be used to increase the clarity of code as shown below: –
age boy, girl;
For Example; the declaration
typedef float lists[20];
lists bpay, sal, gsal;
these statements are equivalent to
float bpay[20], sal[20], gsal[20];
NOTE: – <typedef> is extremely useful for shortening the long and inconvenient structure declaration in C Language.