Evaluation of if's

J H Woodyatt bard at cutter.ssd.loral.com
Fri Jun 7 11:19:38 AEST 1991


In article <20273 at crdgw1.crd.ge.com>, volpe at camelback.crd.ge.com (Christopher R Volpe) writes:
|> In article <1991Jun5.014758.10616 at wdl1.wdl.loral.com>,
|> bard at cutter.ssd.loral.com (J H Woodyatt) writes:
|> |>In article <1991Jun4.233928.5185 at athena.mit.edu>, scs at adam.mit.edu
|> (Steve Summit) writes:
|> |>|> In article <FPB.91Jun4174400 at ittc.ittc.wec.com> fpb at ittc.wec.com
|> (Frank P. Bresz) writes:
|> |>|> >	if ( (i=1) == (i=2) )
|> |>|> >
|> |>|> >Is somebody wrong or is the behavior undefined?
|> |>|> >Personal opinion: I think the behaviour is undefined by K&R anyway
|> I can't
|> |>|> >say for ANSI.
|> |>My 2nd edition K&R book says in the glossary (p.208) that the result of an
|> |>assignment expression is the value that was stored in the lvalue after the
|> |>assignment takes place. Thus, independent of order of evaluation, the above
|> |>expression should always evaluate FALSE. (Or at least so say I.)
|> 
|> Yes, it is undefined according to ANSI. It has nothing to do with order
|> of evaluation, though. You are right about the value of each assignment
|> expression, but that's not the point. The problem, as Steve pointed out,
|> is that the value of "i" is modified twice between sequence points. As
|> a result, all bets are off, since the Standard says that this is not allowed
|> and that anything can happen if you do it.
|> 
|> -Chris
|> 
|> |>|
|> |>| James Woodyatt
|>
|> ==================
|> Chris Volpe
|> G.E. Corporate R&D
|> volpecr at crd.ge.com

<<This is a response to several posts. The above is a general reference.>>

I might submit to you that all bets are off concerning the value stored
in `i' after the expression is evaluated, but as for the result of the
expression, if *that* is undefined, then it isn't by any means intuitive.

And, by the way, I *have* a copy of the FAQ, and I *have* read it. I am
quite aware of the problem of side effects with order of evaluation; I
think the side effect in the above expression renders the value of `i'
undefined, *not* the result of the expression.

I also have a related question. Given the following:

extern volatile int k;
if (k = 1) /* Yes, this is an assignment, not an equivalence test */
    { .... }

I would expect that it is undefined whether the block after the `if'
expression will be executed. Am I right?
 __________________________________________________________________
|
| James Woodyatt                 VOICE:  (415) 852-5429
| Space Systems/Loral (M/S G87)  FAX:    (415) 852-6286
| 3825 Fabian Way                E-MAIL: bard at cutter.ssd.loral.com
| Palo Alto, CA  9430
|



More information about the Comp.lang.c mailing list