Cpp - Data Type typedef 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.


#include <iostream> 
int main() //  w w w. j a  va  2s . c  o m
    // 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;