There are some undefined behavior examples in C/C++ which I was trying on my computer in order to see whether they indeed gave different results each time I ran the code. I noticed that within the same compiler, all the runs gave the same result. So my first question is, is this really undefined behavior?
The second question: How come when I change my compiler from clang to vs, the results change? What can be the root cause of this?
Example 1:
#include <stdio.h>
int main (int argc, char *argv[])
{
int i = 1;
int v = 2;
i = i++ + v;
printf("%d\n", i);
return 0;
}
In Clang and gcc the result is always 3. While in vc, the result is always 4.
Another example with integer overflow:
int main (int argc, char *argv[])
{
int i = 1;
int v = 2;
i = i++ + v;
printf("%d\n", i);
return 0;
}
Clang gives always a totaly diferent result (which I consider as undefined behavior) while gcc always give the same result -2147483648.
I expect the results to vary depending on which machine they use (for example 2 complement) not to vary between compilers.
So my questions in a nutshell: 1) what is ment by undefined behavior and 2) how come the results differ from compiler to compiler?