Article View: comp.lang.c.moderated
Article #2407Re: Explaining precedence vs. sequence
From: Jos A. Horsmeier
Date: Wed, 27 Mar 1996 12:34
Date: Wed, 27 Mar 1996 12:34
100 lines
4027 bytes
4027 bytes
In article <4ja3pu$p8r@solutions.solon.com>, 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)) {.....
|[ ... ] 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.)
|AAR, can someone quote chapter and verse on what covers this
|situation? Any better explanation?
If we're still talking C here, the following applies:
ISO/IEC 9899: Paragraphs 6.3.13 and 6.3.14 Logical and (or) operator
'[ ... ] the && (||) operator guarantees left-to-right evaluation;
there is a sequence point after the evaluation of the first operand.
If the first operand compares (un)equal to 0, the second operand
is not evaluated.'
The following paragraph tells the rest of the story:
ISO/IEC 9899: Paragraph 6.3 Expressions
' Except as indicated by the syntax(35) or otherwise specified later [ ... ]
the order of evaluation of subexpressions and the order in which
side effects take place are both unspecified.'
Footnote 35 reads:
'The syntax spcifies the precedence of operators in the evaluation of
an expression [ .. ].'
Parentheses don't do much actually:
ISO/IEC 9899: Paragraph 6.3.1 Primary expressions
'A parenthesized expression is a primary expression. Its type and value
are identical to thos of the unparenthesized expression.'
Combining your example, the paragraphs above and the syntax of the
individual operators, all we can deduce is, that we can safely remove
the parentheses in the example:
if (value || GetState (value) && dumb == !smart) {.....
but still the conclusion must be: if value != 0 then the rest of
the conditional expression is not evaluated.
If people think that parenthesizing sub-expressions do wonders, show
them this:
#include <stdio.h>
int f() { printf("f"); return 1; }
int g() { printf("g"); return 2; }
int h() { printf("h"); return 3; }
int main() {
printf("f+g*h= %d\n", f()+g()*h());
printf("f+(g*h)= %d\n", f()+(g()*h()));
return 0;
}
Both results of both expressions equal 7, but on my machines (RS6000/320,
SP/2, Sun 690MP, Sun 4/280, Sun Sparc 5, Sun SLC, etc. etc. etc. using cc,
gcc, xlc) function f() is called first during the evaluation of both
expressions ... This just proves that either your fellow students are
all wrong or my machines/compilers _and_ the Standard is wrong ...
But if I translate this last sentence to C, like this:
if (StudWrong || StdrdWrong && MyStuffWrong) { ...
I can see the similarity with your example and my conclusion is that
your fellow students are wrong. ;-)
kind regards,
Jos aka jos@and.nl
--
Atnwgqkrl gy zit vgksr, ug qshiqwtzoeqs!
Technical Details
Message-ID:
<4jbckk$2me@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: jos@and.nl (Jos A. Horsmeier) Newsgroups: comp.lang.c.moderated Subject: Re: Explaining precedence vs. sequence Date: 27 Mar 1996 06:34:28 -0600 Organization: AND Operations Research B.V. Sender: clc@solutions.solon.com Approved: clc@solutions.solon.com Message-ID: <4jbckk$2me@solutions.solon.com> References: <4ja3pu$p8r@solutions.solon.com> NNTP-Posting-Host: solutions.solon.com X-Newsreader: WinVN 0.99.5