How To Do Type Conversion In C++ ?

In certain situations, some variables are declared as integers but sometimes it may be required to get the result as floating point numbers.
The type conversion is to convert the set of declared type to some other required type.
It is easy to convert values from one type to another type in a C++ program.

For example ,
assume that the following type has been declared......
int x;
float y;
The question arises whether....x =y ;
where x is an integer type and y is a floating point type, is a valid assignment.
Since here problem arises of converting of one

variable to other.
For such a case two conversion are carried out:

(1) Converting by assignment : It is a usual way of converting a value from one data type to another by using the assignment operator (equal to sign)

For example assume following thing has been declared:
int x;
float y;
and we have statement x=y;
yes the above statement written will


assign a float value to a integer variable, but such use is not recommended to the programmer, as it will truncate the fractional or real parts and one may not get the desired results......

For this we use the Cast operator

(2) Cast operator : The cast operator is the technique of converting forcefully one data type to another. The operator used is the cast operator and process is known as 'casting'....
The cast operator takes on the format :

(cast-type)expression;or cast-type(expression)

for example, to force a floating point number to integer,
we would use....
result = (int)(19.2/4);
or result = int(19.2/4);


For more of such hot topics read my other posts here :

Visit my new website



Article Written By jontymagicman

I am DInesh Varyani.

Last updated on 28-07-2016 130 0

Please login to comment on this post.
There are no comments yet.
How Global Warming Is Problem Of Concern ?
Important Facts Related To Operating System ?