This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
Bitwise AND short circuit
- From: "Philip C. Tsao" <bender at ugcs dot caltech dot edu>
- To: gcc-bugs at gcc dot gnu dot org
- Date: Wed, 9 Jan 2002 22:55:19 -0800 (PST)
- Subject: Bitwise AND short circuit
We've come across a bit of odd code that generates odd output from GCC
versions 2.7.2 to 3.0.2 (tested on Linux x86 and Sun Sparc).
int main() {
char *s = "abc", c = 1;
int x = 0;
printf( "%d\n", c );
c += 0 & ( x > 0 ? s[ c++ ] : s[ c++ ] );
printf( "\n%d\n", c );
return 0;
}
Pardon the silly code. If the 'x' in the 6th line (c +=...) is
replaced with a constant (say, 0), then the result is different.
If 'x' is left in, gcc figures that 0 & [anything] => 0 and acts as if
it didn't parse the rest of the line. If 'x' is replaced with a
constant, the 'c++' is performed. Is this behavior a compiler bug or are
we tickling some sort of undefined behavior as specified in the ISO
standard?
Thanks