Sunday, December 18, 2011

Type Conversion


TYPE CONVERSION
In certain situations, some variables are declared as integer 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 someother 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 now arises whether, for example,
          x = y;
Where x is an integer type and  y is a floating point type, is a valid assignment.
The answer  to this question is in affirmative, but we should be aware that truncation will take place. This happen most often between float and double, and between int and char, since converting between float and int or double and int involves cutting off all the decimal places. Conversion can be carried out in two ways,
  • Converting by assignment
  • Using cast operator
(a) 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 that the following type has been declared.
             int   x ;
           float  y ;
The question now arises whether

                x = y;

where x is an integer type and y is a floating point data type, a valid assignment statement or not. This means that we can convert a value from one type to another just by assigning a float variable’s value to a double value variable, a char variable’s constant to an int variable or an int variable to a float variable.
    For example,

        int  x,y,z;
       float a,b,c;
       double devalue;
       x = 10 ;
       a =  3546879.908
       dvalue = 3546879.908
       b = x;
       y = a;
       z = dvalue;

   The first one is an integer value assigned back to floating point number. In the second example, the float variable is assigned back to the integer variable, and in the third, the double value is assignment to the float variable.
   In C++, converting by assignment operator is not recommended to the programmer, as it will truncate the frational or real parts and one may not get the desired results. To avoid this, there is a special way of converting one data type to the other, by using the cast operator.
(b)  Cast operator  Converting by assignment operator is carried out automatically but one may not get the desire result. The cast operator is a technique to forcefully convert one data type to the other. The operator used to force this conversion is known as the ‘cast operator’ and the process is known as ‘casting’.
  The cast operator takes on the format.
          (cast-type) expression;
or
           cast-type ( expression);
As an example, to force a floating point number to an integer, we could use the following.
result = (int) (19.2 / 4) ;
or
result = int (19.2 / 4) ;

the cast operation (int)  cast not only the 19.2 but the entire expression. Thus, results value 4, rather than the entire quotient 4.8. The cast operator takes precedence over most other operations.
     To demonstrate the use of the cast operator, let us consider the following example:
          char ch ;
          int  x,y,z;
          float abc;
          x = (int) ch;
  where ch is a character  variable and force to convert the integer data type
          abc = (float) (y*z) ;
where the expression (y*z)  is an integer data type and force to convert as a floating point number.



No comments:

Post a Comment