When do you use "if ( a = b )"? (was Re: Funny mistake)

Andrew Koenig ark at alice.att.com
Sat Mar 30 01:47:11 AEST 1991


In article <3482 at inews.intel.com> bhoughto at hopi.intel.com (Blair P. Houghton) writes:

> No compiler
> in the world won't make
> 	a=b
> 	if(a)
> the same as
> 	if(a=b)
> as far as any user can tell

A while ago I remember a big argument about the difference between
code generated by a particular compiler for this:

	a=b-c;
	if(a)

and for this

	if(a=b-c)

In the first case, the compiler dutifully stored b-c in a and
then tested the value of a.  In the second, it generated a
three-address subtract instruction to put the result in a
and then did a conditional jump based on the condition code
resulting from the subtract.  You'd think those would have the
same effects, wouldn't you?

It turned out that the effects differed if the subtract
caused an overflow (call it underflow if you like) with a
zero result.  In the first case, the test would show that
a==0.  In the second, the condition code would show an
overflow.  Overflow is not the same as zero, so the test
would fail.

I believe that this behavior on the part of the compiler
is entirely reasonable.
-- 
				--Andrew Koenig
				  ark at europa.att.com



More information about the Comp.lang.c mailing list