< previous page page_550 next page >

Page 550
Demotion from a longer integral type to a shorter integral type (such as long to int) results in discarding the leftmost (most significant) bits in the binary number representation. The result may be a drastically different number.
Demotion from a floating point type to an integral type causes truncation of the fractional part (and an undefined result if the whole-number part will not fit into the destination variable). The result of truncating a negative number is machine-dependent.
Demotion from a longer floating point type to a shorter floating point type (such as double to float) may result in a loss of digits of precision.
Our description of type coercion in an assignment operation also holds for parameter passage (the mapping of actual parameters onto formal parameters) and for returning a function value with a return statement. For example, assume that INT_MAX on your machine is 32767 and that you have the following function:
void DoSomething( int n )
{
    .
    .
    .
}
If the function is called with the statement
DoSomething(50000);
then the value 50000 (which is implicitly of type long because it is larger than INT_MAX) is demoted to a completely different, smaller value that fits into an int location. In a similar fashion, execution of the function
int SomeFunc( float x )
{
    .
    .
    .
    return 70000;
}
causes demotion of the value 70000 to a smaller int value because int is the declared type of the function return value.
One interesting consequence of implicit type coercion is the futility of declaring a variable to be unsigned, hoping that the compiler will prevent you from making a mistake like this:

 
< previous page page_550 next page >