Close
Close

typedef in C


typedef keyword is used to assign a new name to a type. This is used just to prevent us from writing more.

For example, if we want to declare some variables of type unsigned int, we have to write unsigned int in a program and it can be quite hectic for some of us. So, we can assign a new name of our choice for unsigned int using typedef which can be used anytime we want to use unsigned int in a program.

typedef current_name new_name;

typedef unsigned int uint;
uint i, j;

Now, we can write uint in the whole program instead of unsigned int. The above code is the same as writing:

unsigned int i, j;

Let's see an example.

#include <stdio.h>
int main() {
	typedef unsigned int ui;
	ui i = 5, j = 8;
	printf("i = %d\n", i);
	printf("j = %d\n", j);
	return 0;
}
Output
i = 5
j = 8

Thus, we can assign a new name to any data type.

Similarly, we can also use typedef to assign a new name to structure which is a user-defined datatype as follows:

typedef struct structure_name
{
  data-type member-1;
  data-type member-2;
  data-type member-3;
  data-type member-4;
}type_name;

Now, while declaring variables of this structure type, we can write type_name in place of struct structure_name in the whole program.

Let's take the example of structure named student which we saw in the Structure topic.

#include <stdio.h>
#include <string.h>
typedef struct student
{
  int roll_no;
  char name[30];
  int phone_number;
}st;
int main()
{
  st p1, p2, p3;
  p1.roll_no = 1;
  strcpy(p1.name,"Brown");
  p1.phone_number = 123443;
  p2.roll_no = 2;
  strcpy(p2.name,"Sam");
  p2.phone_number = 1234567822;
  p3.roll_no = 3;
  strcpy(p3.name,"Addy");
  p3.phone_number = 1234567844;
  printf("First Student\n");
  printf("roll_no : %d\n", p1.roll_no);
  printf("name : %s\n", p1.name);
  printf("phone_number : %d\n", p1.phone_number);
  printf("Second Student\n");
  printf("roll_no : %d\n", p2.roll_no);
  printf("name : %s\n", p2.name);
  printf("phone_number : %d\n", p2.phone_number);
  printf("Third Student\n");
  printf("roll_no : %d\n", p3.roll_no);
  printf("name : %s\n", p3.name);
  printf("phone_number : %d\n", p3.phone_number);
  return 0;
}
Output
First Student
roll_no : 1
name : Brown
phone_number : 123443
Second Student
roll_no : 2
name : Sam
phone_number : 1234567822
Third Student
roll_no : 3
name : Addy
phone_number : 1234567844

Here, the whole example is the same as we did in Structure, the only difference is that we wrote st in place of struct student i.e. we used the new type (named st) to declare the variables of this structure type (named student).

We can also use typedef with unions. For this, everything will be same as that of structure with the keyword union in place of struct.

#include <stdio.h>
#include <string.h>
typedef union student
{
  int roll_no;
  int phone_number;
  char name[30];
}st;
int main()
{
  st p1, p2, p3;
  p1.roll_no = 1;
  p1.phone_number = 123443;
  strcpy(p1.name,"Brown");
  p2.roll_no = 2;
  p2.phone_number = 1234567822;
  strcpy(p2.name,"Sam");
  p3.roll_no = 3;
  p3.phone_number = 1234567844;
  strcpy(p3.name,"Addy");
  printf("First Student\n");
  printf("roll_no : %d\n", p1.roll_no);
  printf("phone_number : %d\n", p1.phone_number);
  printf("name : %s\n", p1.name);
  printf("Second Student\n");
  printf("roll_no : %d\n", p2.roll_no);
  printf("phone_number : %d\n", p2.phone_number);
  printf("name : %s\n", p2.name);
  printf("Third Student\n");
  printf("roll_no : %d\n", p3.roll_no);
  printf("phone_number : %d\n", p3.phone_number);
  printf("name : %s\n", p3.name);
  return 0;
}
Output
First Student
roll_no : 2003792450
phone_number : 2003792450
name : Brown
Second Student
roll_no : 7168339
phone_number : 7168339
name : Sam
Third Student
roll_no : 2036622401
phone_number : 2036622401
name : Addy
To learn from simple videos, you can always look at our C++ video course on CodesDope Pro. It has over 750 practice questions and over 200 solved examples.


Ask Yours
Post Yours
Doubt? Ask question