(fwd) Re: some weird C

Andrey Gerzhov (kittle@freeland.alex-ua.com)
Sun, 31 Jan 1999 17:33:19 +0200 (EET)

-- forwarded message --
Path: freeland.alex-ua.com!news.alexradio.kiev.ua!not-for-mail
From: Kevin Day <toasty@home.dragondata.com>
Message-ID: <199901310744.BAA20866@home.dragondata.com>
Subject: Re: some weird C
To: malartre@aei.ca (Malartre)
Date: Sun, 31 Jan 1999 01:44:10 -0600 (CST)
Newsgroups: alex.gated.freebsd.hackers
Lines: 100
Xref: freeland.alex-ua.com alex.gated.freebsd.hackers:6326

> This is not really.. for hackers@freebsd.org, but I cannot find the
> answer anywhere.
> main()
> {
> int x=4;
> printf("The value of x is %d\n",x);
> printf("The value of \"x += x++\" is %d\n",x += x++);
> x=4;
> printf("The value of x is %d\n",x);
> printf("The value of \"x += ++x\" is %d\n",x += ++x);
> }
>
> The results are:
>
> The value of x is 4
> The value of "x += x++" is 8
> The value of x is 4
> The value of "x += ++x" is 10
>
> I was expecting 9, not 10.
> since 4+5=9?
> Why 10?
> I also noticed that cc is reading from right to left. Cool.
> Thank You
> --
> [Malartre][malartre@aei.ca][http://www.aei.ca/~malartre/]
> [French piss me off - Cartman, South Park][http://9.nws.net/]
>
> To Unsubscribe: send mail to majordomo@FreeBSD.org
> with "unsubscribe freebsd-hackers" in the body of the message
>

Take a look at:

http://www.cis.ohio-state.edu/hypertext/faq/usenet/C-faq/faq/faq.html

Section 3.2

3.2: Under my compiler, the code

int i = 7;
printf("%d\n", i++ * i++);

prints 49. Regardless of the order of evaluation, shouldn't it
print 56?

A: Although the postincrement and postdecrement operators ++ and --
perform their operations after yielding the former value, the
implication of "after" is often misunderstood. It is *not*
guaranteed that an increment or decrement is performed
immediately after giving up the previous value and before any
other part of the expression is evaluated. It is merely
guaranteed that the update will be performed sometime before the
expression is considered "finished" (before the next "sequence
point," in ANSI C's terminology; see question 3.8). In the
example, the compiler chose to multiply the previous value by
itself and to perform both increments afterwards.

The behavior of code which contains multiple, ambiguous side
effects has always been undefined. (Loosely speaking, by
"multiple, ambiguous side effects" we mean any combination of
++, --, =, +=, -=, etc. in a single expression which causes the
same object either to be modified twice or modified and then
inspected. This is a rough definition; see question 3.8 for a
precise one, and question 11.33 for the meaning of "undefined.")
Don't even try to find out how your compiler implements such
things (contrary to the ill-advised exercises in many C
textbooks); as K&R wisely point out, "if you don't know *how*
they are done on various machines, that innocence may help to
protect you."

References: K&R1 Sec. 2.12 p. 50; K&R2 Sec. 2.12 p. 54; ANSI
Sec. 3.3; ISO Sec. 6.3; CT&P Sec. 3.7 p. 47; PCS Sec. 9.5 pp.
120-1.

3.3: I've experimented with the code

int i = 3;
i = i++;

on several compilers. Some gave i the value 3, some gave 4, but
one gave 7. I know the behavior is undefined, but how could it
give 7?

A: Undefined behavior means *anything* can happen. See questions
3.9 and 11.33. (Also, note that neither i++ nor ++i is the same
as i+1. If you want to increment i, use i=i+1, i+=1, i++, or
++i, not some combination. See also question 3.12.)

Kevin

To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-hackers" in the body of the message
-- end of forwarded message --

-- 
С тем, что не помешает никогда,
                                               Kittle