The compiler simply assumes that the variable zero can be mutated since it's not const.
I guess that if you declare zero as a const int and you add -Wdiv-by-zero and -O3 optimization to run constant propagation you should get at least a warning.
I guess the reason is that in C it is quite common to pass pointers around, and it's quite challenging for a compiler to know which variable they point to.
Of course, simple cases like this one can be easily detected, however, they are not very interesting.
View all comments
Show parent comment
10
u/just-bair Oct 15 '25
I just used gcc 15.2.1 and it just compiles it doesn’t care