Would *you* get caught by this one?

Andrew Koenig ark at alice.UUCP
Fri Jan 13 08:04:02 AEST 1989


In article <139 at mole-end.UUCP>, mat at mole-end.UUCP (Mark A Terribile) writes:
> Here's a simple C question on a point that caught me recently.  Would *you*
> get caught?
> 
> 	When is     a *= b      not the same as     a = a * b   ?

Two answers:

	1. When a has side-effects. For example:

		x[i++] *= y;
	
	is not the same as

		x[i++] = x[i++] * y;

	2. When a is fixed-point, b is floating-point, and your
	   compiler is broken.  Many compilers are broken this way.
	   For example:

		int a = 3;
		a *= 2.5;
	
	   A number of compilers will decide to truncate 2.5 to 2
	   and therefore leave a equal to 6 after this is done.
	   The right answer is, of course, 7.  Or maybe 8.  Definitely
	   not 6.

Do I pass?
-- 
				--Andrew Koenig
				  ark at europa.att.com



More information about the Comp.lang.c mailing list