From owner-freebsd-hackers Sat Jan 30 23:46:07 1999 Return-Path: Received: (from majordom@localhost) by hub.freebsd.org (8.8.8/8.8.8) id XAA25248 for freebsd-hackers-outgoing; Sat, 30 Jan 1999 23:46:07 -0800 (PST) (envelope-from owner-freebsd-hackers@FreeBSD.ORG) Received: from panzer.plutotech.com (panzer.plutotech.com [206.168.67.125]) by hub.freebsd.org (8.8.8/8.8.8) with ESMTP id XAA25243 for ; Sat, 30 Jan 1999 23:46:05 -0800 (PST) (envelope-from ken@panzer.plutotech.com) Received: (from ken@localhost) by panzer.plutotech.com (8.9.2/8.8.5) id AAA23838; Sun, 31 Jan 1999 00:45:57 -0700 (MST) From: "Kenneth D. Merry" Message-Id: <199901310745.AAA23838@panzer.plutotech.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 00:45:57 -0700 (MST) 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 Malartre wrote... > 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. See operator(7). The ++ operator is evaluated before the += operator. So, the value of x is changed to 5 by the ++ before it is added to itself. Ken -- Kenneth Merry ken@plutotech.com To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-hackers" in the body of the message