Changing a const
-value yields undefined behaviour, and the “mysterious” output is just such undefined behaviour.
It is actually needless to investigate on why a behaviour in the world of undefined behaviour is as it is then. But in your case it is probably not using a
as you declare it as const
, so the compiler “knows” the value and may decide not to access the variable.
Just for showing something curious, try:
int main()
{
volatile int const a = 1;
int* pa = (int*) &a;
*pa = 2;
printf("%p %d %p %d", (void*) &a, a, (void*) pa, *pa);
return 0;
}
3
solved Variable has two values simultaneously?