-
#include<stdio.h>
int x = 0;
int f1() {
x = 5;
return x;
}
int f2() {
x = 10;
return x;
}
int main() {
int p = f1() + f2();
printf("%d ", x);
return 0;
}
-
#include<stdio.h>
int x = 0;
int f1() {
x = 5;
return x;
}
int f2() {
x = 10;
return x;
}
int main() {
int p = f2() + f1();
printf("%d ", x);
return 0;
}
This is a case of Unspecified Behaviour (Or preferably Compiler Dependent).
Because, Your compiler has the freedom to run the either function $f1$ or $f2$ first . So, your compiler has alternatives to run which one first and it can go with anyone .
Unspecified behaviour comes when your vendor has no idea what compiler will do, hence he/she cannot document that this particular behaviour happens. As, compiler picks any one of the provided alternatives henceforth the name comes Unspecified behaviour .
NOTE :- If you remember race condition from OS
That which thread executes last, will change the output and intermediate threads which run have lost their updates. Same concept is being followed here . Also, note the difference between Unspecified Behaviour and Undefined Behaviour .