When variables of one type are mixed with variables of another type, a type conversion will occur.
In an assignment statement, the type conversion rule is easy: The value of the right side (expression
side) of the assignment is converted to the type of the left side (target variable), as illustrated here:
int x;
char ch;
float f;
void func(void)
{
ch = x;
x = f;
f = ch;
f = x;
}
/* line 1 */
/* line 2 */
/* line 3 */
/* line 4 */
Page 41
In line 1, the left high-order bits of the integer variable x are lopped off, leaving ch with the lower 8
bits. If x were between 255 and 0, ch and x would have identical values. Otherwise, the value of ch
would reflect only the lower-order bits of x. In line 2, x will receive the nonfractional part of f. In
line 3, f will convert the 8-bit integer value stored in ch to the same value in the floating-point
format. This also happens in line 4, except that f will convert an integer value into floating-point
format.
Monday, August 25, 2008
Type Conversion in Assignments
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment