www.delorie.com/djgpp/bugs/show.cgi   search  
Bug 000009

When Created: 07/13/1995 07:11:26
Against DJGPP version: 2.00.beta1
By whom: kenfoo@techm.pl.my
Abstract: if (val=foo() == 0) printf("Hello!\n"); bug?
I'm not fully sure if this is an bug itself.
Anyway, consider this short code.

	if (val=foo() == 0) printf("Hello!");

It works fine in Turbo C++ 1.01. However, when compiled under DJGPP
v2 beta, it gave wrong results. Debugging the assembly code
revealed that value in 'val' isn't the return value of foo().
Instead, it is the assembly test-result value (which is used to
determine whether the return value of foo() is 0).

If I'm not mistaken, DJGPP 1.x did give some warning/error with
this kind of code.

Workaround added: 07/13/1995 07:12:54
By whom: kenfoo@techm.pl.my
As stated earlier, there is a workaround.
Instead of 
	if (val=foo() == 0) printf("Hello!\n");
we use
	if ((val=foo()) == 0) printf("Hello!
);

Solution added: 07/13/1995 11:57:41
By whom: terra@diku.dk
djgpp exhibits correct behaviour here.  If this works with other
compilers you're just plain lucky or the compilers are buggy.

The assignment operator has very low priority.  It should be
evaluated last.

I think using -Wall will cause gcc to warn you about this.

Closed on 07/24/1995 22:27:36: gcc is functioning correctly.
By whom: dj@delorie.com



  webmaster     delorie software   privacy  
  Copyright © 2010   by DJ Delorie     Updated Jul 2010