Why is the output of this C code so unpredictable?
#include <stdio.h>
int a = 1;
int fun(){
a = a * 2;
return a;
}
int main() {
int x = a + fun() + fun();
printf("%d", x);
return 0;
}
I tested this C code on Programiz and it consistently printed 8.
Thing is I got no idea how it's 8 because whether the expression is evaluated from the left or from the right, it just doesn't add up.
Does this depend on the compiler?
I would appreciate clarification on this.