Close
Close

typedef in C++


typedef keyword is used to assign a new name to any existing data-type.

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.

Following is the syntax of typedef

typedef current_name new_name;

Now, suppose we want to declare two variables of type unsigned int. Instead of writing unsigned int again and again, let's use a new name uint in its place using typedef as follows:

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 <iostream>
 int main(){
	typedef unsigned int ui;
	ui i = 5, j = 8;
	std::cout << "i = " << i << std::endl;
	std::cout << "j = " << j << std::endl;
	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 the structure named student which we saw in the Structure topic.

#include <iostream>
#include <cstring>

using namespace std;

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;
  	cout << "First Student" << endl;
  	cout << "roll no : " << p1.roll_no << endl;
  	cout << "name : " << p1.name << endl;
  	cout << "phone no : " << p1.phone_number << endl;
  	cout << "Second Student" << endl;
  	cout << "roll no : " << p2.roll_no << endl;
  	cout << "name : " << p2.name << endl;
  	cout << "phone no : " << p2.phone_number << endl;
  	cout << "Third Student" << endl;
  	cout << "roll no : " << p3.roll_no << endl;
  	cout << "name : " << p3.name << endl;
  	cout << "phone no : " << p3.phone_number << endl;
	return 0;
}
Output
First Student
roll no : 1
name : Brown
phone no : 123443
Second Student
roll no : 2
name : Sam
phone no : 1234567822
Third Student
roll no : 3
name : Addy
phone no : 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 the structure with the keyword union in the place of struct.

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