Hi,
Just want to confirm that the following statements is 'dangerous', in the sense that it's system dependant. I recall to read somewhere there're additional case where this can happen, such as dofun(s, s++).
Any insight on why it happens is highly appreciated.
Here's the program: int main(void) { int s=3; printf("s=%i,s=%i,s=%i\n",s,s++,s--); return 0; }
For the result, I got s=3,s=2,s=3 on SuSE 9.3, but on a Solaris machine, I got s=3,s=3,s=4. Just to add to Anders comments. According to the ANSI 89 standard, this is an unspecified operation. The issue here is when the postincrement operator is applied to s. C pushes variables from the last to the first because it may have an unknown number of parameters. So, the s-- is pushed first, hence the
On Sat, 10 Sep 2005 08:35:12 +0200 (MEST) "Verdi March" <cincaipatron@gmx.net> wrote: third parameter is evaluated as 3. In this case, gcc applies the postdecrement immediately after it pushes the parm on the stack. The second parameter then evaluates to 2 because s now contains 2. Again, gcc increments, resulting in the first parameter as 3. ANSI 3.3.2.2. Another similar issue is i = ++i + 1; The above is an "undefined" operation. (ANSI 3.3). -- Jerry Feldman <gaf@blu.org> Boston Linux and Unix user group http://www.blu.org PGP key id:C5061EA9 PGP Key fingerprint:053C 73EC 3AC1 5C44 3E14 9245 FB00 3ED5 C506 1EA9