Detailed Print – CSKC
Sign In

Search

siteicon
Code Learning#CPP#1747 Back

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

		 

Click to open popup

Run Output

A
10

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