Re: [Code] Brainteaser

From: Daniel A. Koepke (dkoepke@circlemud.org)
Date: 11/21/01


On Wed, 21 Nov 2001, Martijn Schoemaker wrote:

> Good shot, might be that indeed the terms are evaluated left to right
> (as indeed normally is the case). I dunno how the compiler handles
> this with lazy evaluation (if it evaluates humanly from right to left,
> and only lexically from left to right) but you can test it by trying.

This isn't lazy evaluation (that's an entirely different thing), but
short-circuiting.  A conforming C compiler is REQUIRED to evaluate an &&
or || expression left-to-right and to leave the expression as soon as the
overall value is clear.  For an && expression, this means that it leaves
the expression when it encounters a FALSE; for an || expression, this
means that it leaves the expression when it encounters a TRUE.

> It all kinda depends on the compiler.

Not unless you're using a horribly, seriously, terribly broken compiler.
A compiler that does this wrong will not compile 99% of the C code in the
world.

-dak

--
   +---------------------------------------------------------------+
   | FAQ: http://qsilver.queensu.ca/~fletchra/Circle/list-faq.html |
   | Archives: http://post.queensu.ca/listserv/wwwarch/circle.html |
   +---------------------------------------------------------------+



This archive was generated by hypermail 2b30 : 12/06/01 PST