Skip site navigation (1)Skip section navigation (2)
Date:      Sun, 31 Jan 1999 00:45:57 -0700 (MST)
From:      "Kenneth D. Merry" <ken@plutotech.com>
To:        malartre@aei.ca (Malartre)
Cc:        hackers@FreeBSD.ORG
Subject:   Re: some weird C
Message-ID:  <199901310745.AAA23838@panzer.plutotech.com>
In-Reply-To: <36B407F7.1617E743@aei.ca> from Malartre at "Jan 31, 1999  2:36:23 am"

next in thread | previous in thread | raw e-mail | index | archive | help
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



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?199901310745.AAA23838>