This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
Re: y = (++x) + (++x)
- To: panov at iacp dot vl dot ru
- Subject: Re: y = (++x) + (++x)
- From: Michael Meissner <meissner at cygnus dot com>
- Date: Thu, 17 Aug 2000 22:12:35 -0400
- Cc: gcc-bugs at gcc dot gnu dot org
- References: <399C8F95.59904881@iacp.vl.ru>
On Fri, Aug 18, 2000 at 12:21:25PM +1100, Andrey Panov wrote:
> I have compiled following program:
>
> #include<stdio.h>
> main(){
> int x = 1, y;
> y = (++x) + (++x);
> printf("y = %d, x = %d", y, x);
> }
>
> It produces output y = 6, x = 3, while one can expect y = 5, x = 3.
Actually you can't expect y to be 5 or x to be 3. It is undefined behavior if
you modify the same variable twice (or reference a variable which has been
modified) without a sequence point occuring between the two operations.
--
Michael Meissner, Red Hat, Inc.
PMB 198, 174 Littleton Road #3, Westford, Massachusetts 01886, USA
Work: meissner@redhat.com phone: +1 978-486-9304
Non-work: meissner@spectacle-pond.org fax: +1 978-692-4482