Monday, July 1, 2013

Typedef Keyword in C

The typedef keyword is used to create a new name for the existing data type. In other words, we can say that typedef creates a synonym for the existing data type. For example,

typedef int integer;
creates integer as a synonym for integer. Hence we can use integer as a data type for the int.

Example:

integer count;


We have to remember that typedef doesn't create a new data type. It just gives another name for the existing data type.

Generally typedef is used when it is difficult to use the data type. For example, if we have declared some structure node and we want to create a variable for that structure, then we declare it as
struct node p;
It is a bit difficult to use the same notation every time. So if we declare the struct node as Node using typedef, then we can easily represent the data.

For example,
typedef struct node Node;
Then we can declare the struct variables as
Node p;
Thus we can reduce the difficulty in declaring the variables using the typedef keyword.