
On Saturday 10 September 2005 08:35, Verdi March wrote:
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.
The rule is: you can only modify a variable once between two sequence points. The reason is that between two sequence points, the compiler is free to do things in any order. This is why you can't do things like x=x++ This isn't system dependent, it is undefined behaviour. Which translated into English means that the compiler is free to do anything at all (I believe the classic example is that it can cause bats to fly out of your nose) Your example is equivalent to this, because you are modifying s twice between two sequence points. Not allowed. You probably wanted something like printf("s=%i,s=%i,s=%i\n",s,s+1,s-1); Remember that the post/prefix operators ++ and -- actually modifies the variable