Mail Archives: djgpp/1997/08/08/05:32:03
Charles Krug wrote:
> I think that ANSI tries to demand left-to-right evalutaion
> of &&. But not all compilers do this correctly. To
> guarantee left-to-right evaluation, you need:
Wrong. ANSI states that && and || WILL be evaluated left
to right, and that the right hand side will not be executed
if the left is satisfied (false for &&, true for ||).
This is mandated behaviour, any compiler which does not do
this is not ANSI compliant, and a manufacturer attempting
to sell such a compiler as being ANSI compliant could be
sued. Among other things, it will break a lof of the GNU
software including gcc itself.
One of the most common uses of this is in testing array
elements, as in:
if (i >= 0 && i < 10 && a[i] != x)
...
to prevent accessing invalid array elements. This sort of
'defensive' code relies on the compiler working properly.
Chris C
- Raw text -