C programming typedef keyword

We have already learnt about the keywords and data types in c programming. In this tutorial we will be learning the typedef keyword in C programming.

C programming typedef keyword

Consider the following example – Suppose we are storing only roll numbers then it would be very useful and meaningful if we have data type “roll”.

typedef keyword – defining types


int main() 
typedef int Roll;
Roll num1 = 40,num2 = 20;

printf("Roll number 1 : %d",num1);
printf("Roll number 2 : %d",num2);

Explanation of Typedef :

In the above program we want to give another name to an integer data type i.e “roll”. We can do this using typedef statement as below –

typedef int Roll;
Roll num1 = 40,num2 = 20;

However we can also use integer data type after we create another data type “Roll”. typedef keyword would just create similar data type.

Application of typedef keyword

Renaming existing data type

typedef keyword is used to rename the existing data type. suppose you are storing price values in floating variables then it would be useful to have a data type called price, so you can create alias data type of floating point number.

typedef float price

using above statement we have created an alias of floating point data type which will behave similar to floating data type. We can declare variables of price data type using below syntax-

price p1,p2;

Creating structure type

typedef struct
  type member1;
  type member2;
  type member3;

Consider following example –


typedef struct student {
   char name[50];
   int marks;
} stud;

void main() {
   stud s1;

   printf("\nEnter student record\n");

   printf("\nStudent name : ");
   scanf("%s", s1.name);
   printf("\nEnter student marks : ");
   scanf("%d", &s1.marks);

   printf("\nStudent name is %s", s1.name);
   printf("\nRoll is %d", s1.marks);


Whenever we create a structure then we are treated it as new type of data. In C programming typedef keyword gives you facility to rename data type.

In the further chapter (in Structure) we will again go through this chapter in details.

Creating alias for complex type

Suppose you are declaring double pointer like below example –

int **ptr;

It would be helpful and meaningful if you create data type intDoublePointer like below –

typedef int** intDoublePointer;
intDoublePointer p1,p2;

Examples of typedef keyword

Below are some of the examples of typedef keyword in c programming.