Go Back   DaemonForums > Miscellaneous > Programming

Programming C, bash, Python, Perl, PHP, Java, you name it.

Thread Tools Display Modes
Prev Previous Post   Next Post Next
  #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

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

All times are GMT. The time now is 01:41 PM.

Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.
Content copyright © 2007-2010, the authors
Daemon image copyright ©1988, Marshall Kirk McKusick