| www.delorie.com/djgpp/bugs/show.cgi | search |
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.As stated earlier, there is a workaround.
Instead of
if (val=foo() == 0) printf("Hello!\n");
we use
if ((val=foo()) == 0) printf("Hello!
);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.
| webmaster donations bookstore | delorie software privacy |
| Copyright © 2010 by DJ Delorie | Updated Jul 2010 |