This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

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


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]