Article View: comp.lang.c.moderated
Article #2408Re: Explaining precedence vs. sequence
From: Justin C Lloyd
Date: Wed, 27 Mar 1996 12:34
Date: Wed, 27 Mar 1996 12:34
59 lines
3454 bytes
3454 bytes
On 26 Mar 1996 18:57:34 -0600, Bob Calbridge (bcalbridge@dcccd.edu) wrote:
** I'm currently taking a C++ class in order to teach it next Fall.
** I've taught C programming for the last three years so I know a bit
** more than some of the people who program in it that they bring in to
** teach it. The class I'm in got into a discussion that I, as a fellow
** "student" had a hard time explaining to the class.
** The issue revolves around an if statement with a predicate that may
** look like the following:
** if (value || ( GetState (value) && dumb == !smart)) {.....
** The instructor agrees with me, as I'm sure everyone else here would,
** that if value evaluates to true the function GetState() will never
** execute due to short circuiting of the logical operators. A few
** people in the class pointed out that the parenthetical nature of the
** expression on the right of the || should force it to be evaluated
** first. I know that the standard says that logical operators guarantee
** that the evaluation must be performed left to right and stops when the
** expression reaches a guaranteed true or a guaranteed false. I'm not
** positive on this, but I suspect that a logical operator imposes a
** sequence point that forces the evaluation of the left side. (Please
** confirm if this is true.)
** I wrote a short program to prove this and the instructor demoed it in
** class. Unfortunately, I don't believe the instructor is familiar with
** sequence points, though he is aware of their consequences, and has had
** a hard time explaining it to the class. ITMT, some class members are
** losing confidence in the book and its author. Truth to tell, this is
** the first time I've been inclined to write to an author and get him
** straight on some issue. This one tends to over simplify and backs up
** some loose conceptions with bad examples.
** AAR, can someone quote chapter and verse on what covers this
** situation? Any better explanation?
** TIA,
** Bob
Look in sections 7.7 and 7.12.1 of "C: A Reference Manual" (H&S). They
fully explain what you are looking for. I'll quote a bit of it:
Section 7.7 Logical Operator Expressions
"The left operand of || is fully evaluated first. If the value of the left
operand is not equal to zero (in the sense of the != operator), then the
right operand is not evaluated and the result value is 1. If the left
operand is equal to zero, then the right operand is evaluated. The result
value is 1 if the right operand is not equal to zero, and is 0 otherwise."
7.12.1 talks about sequence points and refers to 7.7.
JcL
--
%%%% Justin Lloyd %% lloyd@cs.fsu.edu %% http://www.cs.fsu.edu/~lloyd %%%%
%%% in the face of the blinding sun i wake : Sarah McLachlan %%%
%%% only to find that heaven is a stranger : -- Drawn to -- %%%
%%% place than the world i've left behind : - the Rhythm - %%%
Technical Details
Message-ID:
<4jbck8$2m0@solutions.solon.com>Path:
arc1-ca.pugleaf.net!solon.com!not-for-mailReferences:
<4ja3pu$p8r@solutions.solon.com>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!solon.com!not-for-mail From: lloyd@upsilon.cs.fsu.edu (Justin C Lloyd) Newsgroups: comp.lang.c.moderated Subject: Re: Explaining precedence vs. sequence Date: 27 Mar 1996 06:34:16 -0600 Organization: FSU Computer Science Department Sender: clc@solutions.solon.com Approved: clc@solutions.solon.com Message-ID: <4jbck8$2m0@solutions.solon.com> References: <4ja3pu$p8r@solutions.solon.com> NNTP-Posting-Host: solutions.solon.com X-Newsreader: TIN [version 1.2 PL2]