View Single Post
  #1   (View Single Post)  
Old 18th November 2008
JMJ_coder JMJ_coder is offline
VPN Cryptographer
Join Date: May 2008
Posts: 464
Default Increment ordering


I am writing a program and came across a strange quirk. I'm not sure what the standard is so I thought I asked you. I compiled it on Slackware 12.1 with gcc 4.2.3 and got one result, and compiled it on Solaris (version 10.x, I think) with gcc 3.3.6. Here's the code:

variable1 = variable1++ % 2;
/* same as variable1 = (variable1 % 2) + 1; -- this works on Slackware with gcc 4.2.3 */
/* on Solaris with gcc 3.3.6 - this is the same as variable1 = (variable1 + 1) % 2; */
I need the code to switch between 1 and 2 - it is switching which turn it is in a two player game. I need it to work ultimately on Solaris, so I changed it to:
variable1 = (variable1 % 2) + 1;

But, I'm wondering which is the more correct and closer to standard C? Does one make a difference for code efficiency and optimization (obviously one affects portability)?
And the WORD was made flesh, and dwelt among us. (John 1:14)
Reply With Quote