home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!ferkel.ucsb.edu!taco!gatech!rpi!crdgw1!rdsunx.crd.ge.com!bart!volpe
- From: volpe@bart.NoSubdomain.NoDomain (Christopher R Volpe)
- Newsgroups: comp.lang.c
- Subject: Re: Order of evaluation
- Message-ID: <1992Nov9.181120.4150@crd.ge.com>
- Date: 9 Nov 92 18:11:20 GMT
- References: <qBak-G@ppcger.ppc.sub.org> <1992Nov9.065342.20862@dartvax.dartmouth.edu>
- Sender: volpe@bart (Christopher R Volpe)
- Reply-To: volpe@ausable.crd.ge.com
- Organization: GE Corporate Research & Development
- Lines: 49
- Nntp-Posting-Host: bart.crd.ge.com
-
- In article <1992Nov9.065342.20862@dartvax.dartmouth.edu>, davidg@polvadera.dartmouth.edu (David Gelhar) writes:
- |> In article <qBak-G@ppcger.ppc.sub.org> writes:
- |> >
- |> > So the compiler seems to be allowed to evaluate
- |> >
- |> > a * ( b * c ) instead of a * b * c
- |> >
- |> > but not
- |> >
- |> > a * ( b / c ) instead of a * b / c
- |> >
- |> > Is this correct?
- |> >
- |>
- |> No. The point to remember is that precedence/associativity are NOT the
-
- This is all correct...
-
- |> same thing as order of evaluation. The expression "a * b * c" is required
- |> to be treated like "(a * b) * c", that is, "a" and "b" are multiplied together
- |> and the result is multiplied by "c"; that's the associativity rule. As it
- |> says above, "the precedence and associativity of operands is fully specified".
- |> In other words, the parsing of the expression (what operands go with what
- |> operators) is precisely defined: the operands of the rightmost "*" in this
- |> example are "a*b" and "c". What's undefined is the order of evaluation --
- ^^^^^^^^^ unspecified
-
- |> the subexpressions "a", "b" and "c" are not required to be evaluated in any
- |> particular order.
- |>
- |> The only time order of evaluation matters (indeed, the only time you can
- |> even _tell_ what order is being used, without resorting to looking at
- |> object code) is when the subexpressions have side effects. For example,
- |> in "a() * b() * c()" there's no telling what order the functions may be
- |> called in. This is fine if the functions have no side effects, but if
- |> they do (for example, if they print the function name on the terminal
- |> when called) the results are undefined.
-
- No, the results are not undefined. The result is that the function names
- must be printed out in some order, not necessarily even documented by the
- compiler writer. But they must be printed out. The computer is not allowed
- to turn into a frog at runtime.
-
- -Chris
- --
- ==================
- Chris Volpe
- G.E. Corporate R&D
- volpecr@crd.ge.com
-