CPP Typedef

What is typedef?

We use typedef to create alias name for a variable type:
Source code Result
// Demonstrates typedef keyword
#include <iostream>
// USHORT is by typedef defined to be
// of "unsigned short int" type
typedef unsigned short int USHORT;

int main() {

  using std::cout;
  using std::endl;
// The variables, width, length and area are of USHORT
// type, which is by typedef defined to be
// of "unsigned short int" type.
  USHORT  width = 25;
  USHORT length;
  length = 12;
  USHORT area  = width * length;
  cout << "Width:" << width << "\n";
  cout << "Length: "  << length << endl;
  cout << "Area: " << area <<endl;
  return 0;
Length: 12
Area: 300

It is normal to have all typedef's in a separated header file, which we can include when we need the typedef's.

You can download this example here (needed tools can be found in the right menu on this page).

© 2010 by Finnesand Data. All rights reserved.
This site aims to provide FREE programming training and technics.
Finnesand Data as site owner gives no warranty for the correctness in the pages or source codes.
The risk of using this web-site pages or any program codes from this website is entirely at the individual user.