Sender: crough45 AT amc DOT de Message-Id: <97Aug8.112626gmt+0100.17059@internet01.amc.de> Date: Fri, 8 Aug 1997 10:29:01 +0100 From: Chris Croughton Mime-Version: 1.0 To: charles AT pentek DOT com Cc: djgpp AT delorie DOT com Subject: Re: [Q] expression evaluation order Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Precedence: bulk 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