Typedef in C++

Typedef

Here, on this page, we will discuss typedef in c++. We can create alias names to the existing datatype name (predefined or user-defined) with the help of typedef keyword. It doesn’t create a new datatype It just adds a new name for the existing type. Sometimes when we are using complex type names more frequently in our program, it is better to  have a simple alternate name to increase the readability of the code

typedef in c++

C++ typedef Syntax

typedef built_in_datatype user_defined_name;

Here existing name is the C++ datatype and user_defined_name is the alternate name given to existing_type

C++ program to  demonstrate typedef

Run
#include <iostream>
using namespace std;

int main() 
{
    typedef int trish;
    
    trish a = 1; //a is int type internally 
    trish b = 2; //b is int type internally
    
    cout << "a: " << a << "\nb: " << b;
    
    return 0;
}
						
Output:
a: 1
b: 2

Using typedef for struct types

Here we have used the student as an alias name for the struct name stuAlias, so where ever you create a variable with type student internally structure is created and that variable will have size same structure
Run
#include<iostream.h>
using namespace std;

// typedef struct created here
typedef struct stuAlias
{
    string name;
    int id;
    string klg;
}student;

int main()
{

    student s1,s2;  //2 variables of type student
    
    s1.id = 66;
    s1.name = "trishaank";
    s1.klg = "MECS";
    
    s2.id = 27;
    s2.klg = "KMIT";
    s2.name = "Rishi";
    
    cout << "Trishaank Details:\n";
    cout << s1.name << " "<< s1.id << " "<< s1.klg << endl;

    cout << "\nRishi Details:\n";
    cout << s2.name << " " << s2.id <<" "<< s2.klg;

    return 0;
}

Output

Trishaank Details: 
trishaank 66 MECS 

Rishi Details: 
Rishi 27 KMIT

Nested typedef

Once you create an alias name for a datatype using a typedef, you can further be given another alias name with the previous alias name but finally, Compiler Replaces the with root alias
  • Here trish is replaced with int but whenever compiler encounters the name rish it initially replaces with trish then replaces with int
  • Similarly, when vicky is encountered it is replaced with rish initially, as no datatype, is  found with that name it replaces with trish and finally, trish is replaced with int
Run
#include<iostream>
using namespace std;

int main()
{
    typedef int trish;
    typedef trish rish;
    typedef rish vicky;

    trish a = 1;
    rish b = 2;
    vicky c = 3;

    //trish, rish, vicky treated as int internally
    cout << "a:" << a << "\t" << "b:" << b << "\t" << "c:" << c;

    return 0;
}

Output

a:1
b:2
c:3

Note:

You can freely use the original name even if you have an alias name within the program
Run
#include <iostream>
{
    typedef int trish;
    trish a = 1;  // a is int type internally
    int b = 2;  // you can use original datatype name
}						

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription