Typedef
C++ allows you to define explicitly new data type names by using the keyword typedef. Using typedef does not create a new data class, rather it defines a name for an existing type. This can increase the portability(the ability of a program to be used across different types of machines; i.e., mini, mainframe, micro, etc; without many changes to the code)of a program as only the typedef statements would have to be changed. Using typedef one can also aid in self-documenting code by allowing descriptive names for the standard data types.
Syntax
typedef typeName;
where typeName is any C++ data type and name is the new name for this data type. This defines another name for the standard type of C++.
Example:
The below program demonstrates the type def in C++.
CPP
// C++ program to demonstrate typedef #include <iostream> using namespace std; // After this line BYTE can be used // in place of unsigned char typedef unsigned char BYTE ; int main() { BYTE b1, b2; b1 = 'c' ; cout << " " << b1; return 0; } |
c
User Defined Data Types in C++
Data types are means to identify the type of data and associated operations of handling it. improve. In C++ datatypes are used to declare the variable. There are three types of data types:
- Pre-defined DataTypes
- Derived Data Types
- User-defined DataTypes
In this article, the User-Defined DataType is explained:
Contact Us