Typecasting while outputting
Type casting is often useful for manipulating display while outputting data.
Learning Objectives
- Learning to manipulate the display of data while outputting using typecasting.
Source Code
|
Run Output
Code Understanding
int a=65; cout<<(char) a<<endl;
Here integer variable a is initialised with a value 65 which is ascii equivalent of uppercase A. while outputting we can print A by simply typecasting it as a (char) type.
float b=10.32; cout<<(int) b<<endl;
Here a float variable is initialised with 10.32. If we want to show the integer portion we can typecast is as an (int) to show only the value before the decimal point. So the output will come as 10.
Notes
- Some times the precision of value may be affected when a data is type-casted. For e.g. typecasting a float value to a double number may display results different from the expected results.
Suggested Filename(s): output-cast.cpp
CSKC| Created: 16-Dec-2017 | Updated: 28-Aug-2018|