www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/06/21/23:05:44

From: Andrew Crabtree <andrewc AT typhoon DOT rose DOT hp DOT com>
Message-Id: <199706220305.AA066958721@typhoon.rose.hp.com>
Subject: Re: Is this the normal behavior?
To: edevaldo AT sysnetway DOT com DOT br (Edevaldo)
Date: Sat, 21 Jun 1997 20:05:21 PDT
Cc: djgpp AT delorie DOT com
In-Reply-To: <33ABA2A0.539A@sysnetway.com.br>; from "Edevaldo" at Jun 21, 97 6:45 pm

 
>         for( i=0; i<4; printf( "%i %i\n", i, i++) );
I can't remember exactly, but I don't think you are allowed to
reference i in both places there when you modify its value.
Its one of those screwy 'undefined' outcomes in ANSI I believe.

> 
> 1 0
> 2 1
> 3 2
> 4 3
> 
> 	Is this the normal behavior?
> 	In C the later arguments are evaluated first? The stack?

Assuming you are using a machine that does stack based passing, then
function arguments are evaluated and passed from right to left.  This
is specifically for printf style functions, where the left most argument 
tells you how many others to expect.  So, in this case, gcc passes the 
value of I onto the stack (the rightmost one), then increments it,
then passes the left i onto the stack.

There's other gotchas like it.  Try

i=10
printf("New Value = %d\n",++i + ++i);

Andrew 

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019