+ 2
What is the concept of type conversion ?
3 Respuestas
+ 3
Jahnavi Shanbhag
Type conversion is of two types:
1) implicit type conversion
2) explicit type conversions
Implicit Type Conversion: There are certain cases in which data will get automatically converted from one type to another:When data is being stored in a variable, if the data being stored does not match the type of the variable.The data being stored will be converted to match the type of the storage variable.When an operation is being performed on data of two different types. The "smaller" data type will be converted to match the "larger" type.
The following example converts the value of x to a double precision value before performing the division. Note that if the 3.0 were changed to a simple 3, then integer division would be performed, losing any fractional values in the result.average = x / 3.0;When data is passed to or returned from functions.
+ 3
Explicit Type Conversion: Data may also be expressly converted, using the typecast operator.The following example converts the value of x to a double precision value before performing the division. ( y will then be implicitly promoted, following the guidelines listed above. )average = ( double ) x / y;Note that x itself is unaffected by this conversion.