Introduction
Type conversion occurs when the expression has data of mixed data types, for example, converting an integer value into a float value, or assigning the value of the expression to a variable with different data types.
Program/Example
In type conversion, the data type is promoted from lower to higher because converting higher to lower involves loss of precision and value.
For type conversion, C maintains a hierarchy of data types using the following rules:
Integer types are lower than floating-point types.
Signed types are lower than unsigned types.
Short whole-number types are lower than longer types.
The hierarchy of data types is as follows: double, float, long, int, short, char.
These general rules are accompanied by specific rules, as follows:
If the mixed expression is of the double data type, the other operand is also converted to double and the result will be double.
If the mixed expression is of the unsigned long data type, then the other operand is also converted to double and the result will be double.
Float is promoted to double.
If the expression includes long and unsigned integer data types, the unsigned integer is converted to unsigned long and the result will be unsigned long.
If the expression contains long and any other data type, that data type is converted to long and the result will be long.
If the expression includes unsigned integer and any other data type, the other data type is converted to an unsigned integer and the result will be unsigned integer.
Character and short data are promoted to integer.
Unsigned char and unsigned short are converted to unsigned integer.
Type conversion occurs when the expression has data of mixed data types, for example, converting an integer value into a float value, or assigning the value of the expression to a variable with different data types.
Program/Example
In type conversion, the data type is promoted from lower to higher because converting higher to lower involves loss of precision and value.
For type conversion, C maintains a hierarchy of data types using the following rules:
Integer types are lower than floating-point types.
Signed types are lower than unsigned types.
Short whole-number types are lower than longer types.
The hierarchy of data types is as follows: double, float, long, int, short, char.
These general rules are accompanied by specific rules, as follows:
If the mixed expression is of the double data type, the other operand is also converted to double and the result will be double.
If the mixed expression is of the unsigned long data type, then the other operand is also converted to double and the result will be double.
Float is promoted to double.
If the expression includes long and unsigned integer data types, the unsigned integer is converted to unsigned long and the result will be unsigned long.
If the expression contains long and any other data type, that data type is converted to long and the result will be long.
If the expression includes unsigned integer and any other data type, the other data type is converted to an unsigned integer and the result will be unsigned integer.
Character and short data are promoted to integer.
Unsigned char and unsigned short are converted to unsigned integer.
No comments:
Post a Comment