codescracker


c++

C++ typedef



« Previous Tutorial Next Tutorial »


C++ allows you to define explicitly new data type names by using the keyword typedef. Using typedef doest not actually create a new data class, rather it defines a new name for an existing type. This can increase the portability of a programas only the typedef statements would have to be changed.

Using typedef can also aid in self-documenting your code by allowing descriptive names for the standard data types.

C++ typedef Syntax

The syntax of the typedef statement is :

typedef type name;

where type is any C++ data type and name is the new name for this type. This defines another name for the standard type of C++. For example, you would create a new name for float values by using the following statement :

typedef float amount;

New name for the type float has be created through typedef.

This statement tells the compiler to recognize amount as an alternative name for float. Now you could create float variables using amount.

amount loan, saving, instalment;

See, variable for type amount (a typedef name for float) being created.

Here loan, saving, instalment are variables of type amount which is another word for float.
Thus, loan, saving, instalment are all float variables, internally.

Note - Remember that typedef does not create any new data types rather provides an alternative name for standard types. Reference provides an alias name for a variable and typedef provides an alias name for a data type. Let's take an example, demonstrating typedef in C++

C++ typedef Example

Here is an example program, illustrating typedef in C++

/* C++ typedef - Example Program of typedef in C++ */

#include<iostream.h>
#include<conio.h>
void main()
{
	clrscr();

	typedef int integer;
	/* now you can easily use integer to create
	 * variables of type int like this */
	integer num1, num2, sum;
	cout<<"Enter two number: ";
	cin>>num1>>num2;
	sum=num1+num2;
	cout<<"Sum = "<<sum;

	getch();
}

Here is the sample run of the above C++ program:

c++ typedef

Here is another program, also illustrating typedef in C++

/* C++ typedef - Example Program of typedef in C++ */

#include<iostream.h>
#include<conio.h>
void main()
{
	clrscr();

	typedef int integer;
	/* now you can easily use integer to create
	 * variables of type int like this */
	integer num1;

	/* Remember that, you are always free to use
	 * the original keyword to define variable
	 * of int/other data type at any time like
	 * this */
	int num2, sum;
	cout<<"Enter two number: ";
	cin>>num1>>num2;
	sum=num1+num2;
	cout<<"Sum = "<<sum;

	getch();
}

Here is the sample run of this C++ program:

c++ typedef example program

Let's take another program, for complete understanding on typedef in C++. This program, uses typedef to create another name for int. And uses, this name to create again another name for the same data type which is int. And, using this name, creates again another name for the data type int using the typedef. The names are integer, integer_type and then integer_data_type, all the three names uses to create variable of type int, as shown in this C++ program:

/* C++ typedef - Example Program of typedef in C++ */

#include<iostream.h>
#include<conio.h>
void main()
{
	clrscr();

	typedef int integer;
	integer num1;
	typedef integer integer_type;
	integer_type num2;
	typedef integer_type integer_data_type;
	integer_data_type sum;

	cout<<"num1 = ";
	cin>>num1;
	cout<<"num2 = ";
	cin>>num2;
	sum=num1+num2;
	cout<<"Sum = "<<sum;

	getch();
}

Below is the sample run of the above C++ program:

typedef c++

« Previous Tutorial Next Tutorial »



Tools
Calculator

Quick Links
Signup - Login - Give Online Test