Cpp - Data Type typedef Introduction

Introduction

You can use typedef to create new type.

A typedef requires typedef followed by the existing type and its new name. Here's an example:

typedef unsigned short USHORT 

This statement creates a type definition named USHORT that can be used anywhere in a program in place of unsigned short.

The following code shows how to use typedef.

Demo

#include <iostream> 
 
int main() /*from ww  w  . ja v  a2  s . c  om*/
{ 
    // create a type definition 
    typedef unsigned short USHORT;  
 
    // set up width and length 
    USHORT width = 26; 
    USHORT length = 40; 

    // create an unsigned short initialized with the 
    // result of multiplying width by length 
    USHORT area = width * length; 
 
    std::cout << "Width: " << width << "\n"; 
    std::cout << "Length: "  << length << "\n"; 
    std::cout << "Area: " << area << "\n"; 
    return 0; 
}

Result

Exercise