C code gives different results in OS X vs Linux

I am trying to execute the following C code:

#include <stdio.h>

int a = 5;
int fun1(){
  a = 17;
  return 3;
}

int main(){
  int b;
  b = a + fun1();
  printf("%d\n", b);
}

When I run it on my macbook, I get a response from 8, but when I run it on Linux, I get a response of 20. I had several friends who ran it, and everyone with a Mac gets 8, and all Linux starts getting 20 What can cause this?

I am not so interested in the correct answer, because I am in the reason that the two environments give different answers. What about OS X and Linux cause mismatch?

+4
source share
3 answers

+ . , , fun1() a a + fun1() *. .


* , fun1() , a + fun1(); , . (, a + a++), undefined.

+8

, , . , a + fun1() fun1(). . .

+3

There is no information on how to evaluate these operands, so the compiler can select any order. It seems that in this case the compiler that you use on your Mac has a different implementation for the evaluation order than the compiler on your Linux, and therefore the apparent inconsistency. Writing a cleanup code helps a lot in such things.

+1
source

All Articles