What is Typedef in C++

In Programming there are many cases when you declare same data types variables many time. Every time when you declare a variable with its datatypes reduce the readability and increase your keystrokes. Typedef is a function in which you declare the datatypes with its name (typedef name) and next time you can use that typedef name in your program everywhere. For better understanding we first declare five unsigned short integers and after this used typedef and declare five long int with typedef . Here is the source code of this problem that helps you in better understanding.

Source Code

#include<iostream>
using namespace std;
typedef long int L; 
int main ()
{
 unsigned short int a =1;      // Not good to declare every time 
 unsigned short int b =2;
 unsigned short int c =3;
 unsigned short int d =4;
 unsigned short int e =5;
 L ab = 22;           // declare with typedef increase the readability of the code
 L bc = 34;
 L cd = 32;
 L de = 12;
 L ef = 234;
 cout<<" typedef is Used to Save Kepstokes ! \n ";
 return 0;
}

Output of the Program

Typedef in C++


Share on Google Plus

About Asad

Asad Niazi is Software Engineer , Programmer, Web Developer and a young mentor of BloggersTown and PProgramming. Asad Love to writes about Technology, Programming, Blogging and make money online.
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment