From owner-freebsd-hackers Sat Jan 30 23:44:15 1999 Return-Path: Received: (from majordom@localhost) by hub.freebsd.org (8.8.8/8.8.8) id XAA24805 for freebsd-hackers-outgoing; Sat, 30 Jan 1999 23:44:15 -0800 (PST) (envelope-from owner-freebsd-hackers@FreeBSD.ORG) Received: from home.dragondata.com (home.dragondata.com [204.137.237.2]) by hub.freebsd.org (8.8.8/8.8.8) with ESMTP id XAA24800 for ; Sat, 30 Jan 1999 23:44:14 -0800 (PST) (envelope-from toasty@home.dragondata.com) Received: (from toasty@localhost) by home.dragondata.com (8.9.2/8.9.2) id BAA20866; Sun, 31 Jan 1999 01:44:11 -0600 (CST) From: Kevin Day Message-Id: <199901310744.BAA20866@home.dragondata.com> Subject: Re: some weird C In-Reply-To: <36B407F7.1617E743@aei.ca> from Malartre at "Jan 31, 1999 2:36:23 am" To: malartre@aei.ca (Malartre) Date: Sun, 31 Jan 1999 01:44:10 -0600 (CST) Cc: hackers@FreeBSD.ORG X-Mailer: ELM [version 2.4ME+ PL43 (25)] MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Sender: owner-freebsd-hackers@FreeBSD.ORG Precedence: bulk X-Loop: FreeBSD.ORG > 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