There's not a while lot of mystery here. By using casts to tell the compiler that what you're changing isn't const
qualified, you're causing undefined behavior:
6.7.3/5 "Type qualifiers" (C99):
If an attempt is made to modify an object defined with a const-qualified type through use
of an lvalue with non-const-qualified type, the behavior is undefined.
Some implementations might have placed the variable z
in read only memory and you'd either get no apparent change or some sort of access violation.
In any case, undefined behavior means all bets are off - in your case you're able to see the apparent modification of a const
value.