Monday, October 27

TYPE CONVERSION


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);
Thus this ends our topic of TYPE CONVERSION , come everyday for some great topics.....
In my next post i will tell you about writing a program in C++ (basic)......

0 comments:

Followers

Archive

 

Money ,Entertainment and Education. Copyright 2008 All Rights Reserved Revolution Two Church theme by Brian Gardner Converted into Blogger Template by Bloganol dot com