Hello,
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:
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:
Code:
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)?