typedef data type in C++ with examples

typedef type data type in C++, we can represent a data-type, by a different name that is, an identifier can be represented from the data type,

Remember here the name of the data type is being changed, rather than creating a new data type, that means it used to represent an already declared data type name rather creating new data type.

syntax

typedef data-type identifier;

how to use typedef data-type in a C++ program?

firstly, we declare an identifier as data-type in a single with typedef keyword statement such as,

typedef int numeric;

now, numeric behaves like an int data-type,

numeric num =24;

Let’s try this an example here,

As we already know, the int type data type stores the numeric types value but here we define numeric as int with the help of typedef now numeric will be used instead of int data-type.

#include<iostream>
using namespce std;
int main()
  {
    typedef int number;

    number num = 10;

    cout<<"Enter Number: ";
    cin>>num;

      cout<<num; 

   return 0;
 }

OUTPUT

Enter Number: 10

Similarly, you can also use typedef data-type with other data-type(char, float).

Here is another where we used typedef To creating a Progress Bar in C++ with the help of array.


Previous– Type modifier in C++
Next- if-else statement in C++


Like it?

Leave a Reply

Your email address will not be published.