Use of typedef – 1
typedef as replacement for using a friendlier name.
Learning Objectives
- Use of typedef as replacement of a friendlier name for a primitive datatype..
Source Code
|
Run Output
Code Understanding
typedef double amount;
Here the name amount can be used as a replacement of primitive datatype double.
amount principal=100.50;
amount term=2.5;
amount rate=5.5;
All these are declaration and initialization based on a friendlier replacement name.
amount simple_interest=principal*term*rate/100.0;
Here we use the friendlier name for output of the expression as well.
Notes
- When typedef is used, the standard primitive type can still be used. For e.g. double can still be used for definitions.
Common Errors
- Sometime student write primitive data type after the friendly name. The standard syntax is –
typedef <primitive datatype> <friendly name>
Suggested Filename(s): typedef1.cpp
sunmitra| Created: 23-May-2018 | Updated: 27-Aug-2018|