problem in type conversion

Michael O'Brien obrien at randvax.ARPA
Sat Dec 31 11:20:11 AEST 1983


We think there may be a serious problem with type conversion.  Consider the
following program:

int i = 6, j = 6;
main() {
	i *= .5;
	j = j * .5;
	printf ("%d, %d\n", i, j);
}

This program will show i = 0 and j = 3.  The problem is that the ".5" in
the first line is converted to an integer before the multiplication takes
place, and in the second line, the multiplication is done in double precision
before the integer conversion occurs.

This seems to violate Kernighan and Ritchie, which states that

	E1 op= E2

may be regarded as identical to

	E1 = E1 op (E2);

when E1 and E2 are of arithmetic type, except that E1 is evaluated only
once in the first case.  That single difference does not seem to imply such
an egregious difference in type conversion.

We see this both in 4.1 and 4.2 BSD PCC, and in the Ritchie compiler for V7
on the PDP-11.  We don't have System III or System V compilers handy.

Any comments?



More information about the Comp.lang.c mailing list