[comp.lang.c] unbelievable "simple" question/bug?

jjk@jupiter.astro.umd.edu (Jim Klavetter) (03/28/91)

It isn't precedence or some side effect, it is just weird.  I have the
code fragment (a, b, c, integers)

	if(a-b-c>0)
		do ifstuff
	else
		do elsestuff

When a=b=c=1 the above should translate to if(-1>0) and the elsestuff
should get done.  It doesn't.  I tried putting parens around (a-b-c)
(which should be unnecessaray) and it didn't make any difference.  OK,
now here is the real weird part:  if I have the following:
	temp=a-b-c;
	if(temp>0)
		do ifstuff
	else
		do elsestuff
it works as expected!  Anybody want to tell me what I'm missing?

Obviously, this really isn't the exact code, so I'll give a few more
details which I don't think are relevant.  I'm using gcc on a sun4
running sunos 4.1.  "a" is really argc and "b" is really optind but I
know that isn't relevant because I check their values (as well as "c")
both before the if and in both branches and all three values don't
change (as expected).

Thanks for any help possible with this one.

jjk@astro.umd.edu also for Athabasca and Reudi
Jim Klavetter
Astronomy
UMD
College Park, MD  20742

volpe@camelback.crd.ge.com (Christopher R Volpe) (03/29/91)

In article <8331@umd5.umd.edu>, jjk@jupiter.astro.umd.edu (Jim
Klavetter) writes:
|>It isn't precedence or some side effect, it is just weird.  I have the
|>code fragment (a, b, c, integers)
|>
|>	if(a-b-c>0)
|>		do ifstuff
|>	else
|>		do elsestuff
|>
|>When a=b=c=1 the above should translate to if(-1>0) and the elsestuff
|>should get done.  It doesn't.  I tried putting parens around (a-b-c)
|>(which should be unnecessaray) and it didn't make any difference.  OK,
|>now here is the real weird part:  if I have the following:
|>	temp=a-b-c;
|>	if(temp>0)
|>		do ifstuff
|>	else
|>		do elsestuff
|>it works as expected!  Anybody want to tell me what I'm missing?

Are you sure they are all ints? If one of either a or b or c is really
an UNSIGNED int, then a-b-c would be some big positive number, which
would explain the first example. If temp is an int, then putting that
big positive integer back in temp would do what you want, since temp 
would then have the value -1.                                 
==================
Chris Volpe
G.E. Corporate R&D
volpecr@crd.ge.com