Evaluation of if's

Checkpoint Technologies ckp at grebyn.com
Wed Jun 5 22:39:31 AEST 1991


In article <1991Jun4.233928.5185 at athena.mit.edu> scs at adam.mit.edu 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.
>
>Of course it's undefined.  (It contains two side effects not
>separated by a sequence point.)

You know, that was my first impression.  But after thinking about it,
I'm not so sure.

You see, I think the (i=1) should evaluate to the value 1, and the (i=2)
should evaluate to the value 2, regardless of the order in which they're
performed.  The order of operations will give the resulting value for i
after the if statement. The variable i is not fetched during the entire
statement, so the order in which the variable is stored should not
matter.  Unless someone thinks the compiler should evaluate (i = 1) by
storing 1 in i, then fetch that value back some time later for the
comparison, but I seem to recall some ANSI rules about how many times a
storage object may be touched between sequence points.

-- 
Richard Krehbiel, private citizen      ckp at grebyn.com
(Who needs a fancy .signature?)



More information about the Comp.lang.c mailing list