delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/08/08/11:04:59

From: Erik Max Francis <max AT alcyone DOT com>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: [Q] expression evaluation order
Date: Thu, 07 Aug 1997 21:14:48 -0700
Organization: Alcyone Systems
Lines: 36
Message-ID: <33EA9D38.31561027@alcyone.com>
References: <5sbb1h$kvr$1 AT newton DOT pacific DOT net DOT sg> <5sd0pd$k66$1 AT gte1 DOT gte DOT net>
NNTP-Posting-Host: newton.alcyone.com
Mime-Version: 1.0
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

Kenneth Loafman wrote:

> The best example of this is "if (p && *p == 'a') {}" where the NULLness
> of
> p would be checked in the first expression before deferencing it in the
> second expression.
> 
> Although not guaranteed, most C's stop evaluation once an ORing
> expression
> returns true from one of its expressions.  Another potential surprise.

Where in the world do you get this?  ANSI C guarantees short circuiting
for both && and ||.  Section 6.3.13:

    Unlike the bitwise binary & operator, the && operator guarantees
    left-to-right evaluation; there is a sequence point afte the
    evaluation of the first operand.  If the first operand compares equal
    to 0, the second operand is not evaluated.

This guarantees short circuiting for those who now how read a standard.
And section 6.3.14 decribes short circuiting for || ("if the first operand
compares _unequal_ to 0 . . ." [emphasis mine]).

I have yet to see a C compiler which claims ANSI compliance which does
_not_ have short circuiting features for both && and ||.  Short circuiting
isn't new in ANSI, nor in K&R; it was in traditional C, and in fact short
circuiting is the reason there is a distinction between the logical and
bitwise operators.

-- 
       Erik Max Francis, &tSftDotIotE / email / mailto:max AT alcyone DOT com
                     Alcyone Systems /   web / http://www.alcyone.com/max/
San Jose, California, United States /  icbm / 37 20 07 N  121 53 38 W
                                   \
   "Love is not love which alters / when it alteration finds."
                                 / William Shakespeare, _Sonnets_, 116

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019