Incomplete standards

Saturday 5. April, 2008

My friend does some C programming and many times he has im’ed me about his progress in his projects or other interesting results. This time he complained that his code doesn’t work the same way in Linux as it does in Windows. I’ll demonstrate the problem with the same code he demonstrated the problem to me:

#include <stdio.h>

int main(int argc, char *argv[]) {
int temp = 0;
printf(“%d %d “, temp++, temp++);
printf(“%d\n”, temp);
return 0;
}

What do you expect this little program to do? Does it print something? You’re right but what exactly does it print? well, that depends on what compiler it was compiled on…

WIth University’s Sparc-Solaris gcc-3.0.4 it print 0 1 2, with Ubuntu Feisty’s gcc-4.1.2 and lcc-win32 it will print 1 0 2 and actually lcc-win32 printed the same as did Cygwin’s gcc-4.4.3.

Googling for explanation ended up telling that this case is not defined in standard C so results may vary on every compiler.

I wonder why that hasn’t been defined… For a programmer it should be quite clear that this code should print 0 1 2 but I do understand that this might get bit controversial as you could expect it to print anything from 0 0 1, 0 0 2, and so on to 0 1 2.

(Ha! You might have expected me to write about my lack of morality and standards instead of this…)

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: