HP C A.06.05 Reference Manual

Expressions and Operators
Assignment Operators (=, +=, -=, *=, /=, %=,<<=, >>=, &=, ^=, |=)
Chapter 590
assigns to j, the remainder of
71124 / (65535+1)
The remainder is 5588. For non-negative numbers, and for negative numbers represented in
two's complement notation, this is the same result that you would obtain by ignoring the
extra bytes.
Integer to Float Conversions You may assign an integer value to a floating-point
variable. In this case, the integer value is implicitly converted to a floating-point type. If the
floating-point type is capable of representing the integer, there is no change in value. If f is a
double, the assignment
f = 10;
is executed as if it had been written
f = 10.0;
This conversion is invisible. There are cases, however, where a floating-point type is not
capable of exactly representing all integer values. Even though the range of floating-point
values is generally greater than the range of integer values, the precision may not be as good
for large numbers. In these instances, conversion of an integer to a floating-point value may
result in a loss of precision. Consider the following example:
#include <stdio.h>
int main(void)
{
long int j = 2147483600;
float x;
x = j;
printf("j is %d\nx is %10f\n", j, x);
exit(0);
}
If you compile and execute this program, you get:
j is 2147483600
x is 2147483648.000000
Float to Integer Conversions The most risky mixture of integer and floating-point values
is the case where a floating-point value is assigned to an integer variable. First, the fractional
part is discarded. Then, if the resulting integer can fit in the integer variable, the assignment
is made. In the following statement, assuming j is an int, the double value 2.5 is converted to
the int value 2 before it is assigned.
j = 2.5;