2

Is there a difference b/w gcc on linux and windows For example:

#include<stdio.h>

main()
{
  int i=5;
  printf("%d  %d ",++i,++i);
}

On Windows it is 6 5 On Linux it comes to be 7 7

Raman Singh
  • 329
  • 7
  • 17
  • 15
    I am guessing the `++j` is really `++i` which in that case makes this undefined behavior since you are not allowed to modify the same variable more than once within a sequence point. – Shafik Yaghmour Oct 06 '13 at 03:01
  • Uh huh. And what is j? – Charlie Burns Oct 06 '13 at 03:02
  • Are they the same version of GCC? – Barmar Oct 06 '13 at 03:02
  • 1
    Your code has a bug. Fix the bug and the mystery will go away. – David Schwartz Oct 06 '13 at 03:04
  • @DavidSchwartz incidentally, osx gives `6 7` when fixing the bug – SheetJS Oct 06 '13 at 03:06
  • 2
    Though mine Gives [Error] stackoverflow.c:6: error: `j' was not declared in this scope. That is undefined behavior. There are no guarantee of what will happen at all both in Windows & Linux . – internals-in Oct 06 '13 at 03:12
  • 1
    Just see compiler warning: `operation on 'i' may be undefined`. – boleto Oct 06 '13 at 03:32
  • @Nope -- As alluded to by Shafik Yaghmour, try doing a google search on: sequence points C programming – willus Oct 06 '13 at 03:43
  • @Moderators - it looks like this question has been updated - I think it is now a perfect valid question which could do with a good answer. – Digital Trauma Oct 06 '13 at 20:42
  • The answer here will basically come down to the fact that it entirely undefined what the compiler should do here. So different compilers, different versions of the same compiler and even the same version of same compiler on different arch/OS can have different behaviour. The only thing we can be sure of is that the value of i *will* be 7 *after* the printf() statement. – Digital Trauma Oct 06 '13 at 20:47
  • See my answer [here](http://stackoverflow.com/a/18260171/1708801) for more details. – Shafik Yaghmour Oct 09 '13 at 16:49

0 Answers0