this question has answer here:
#include <stdio.h> int main(){ const int = 10; *(int*)(&a) = 9; // modify printf("%d", a); return 0; }
- when run code on xcode, output 10 (not changed)
- when run code on visual studio community, output 9 (changed)
why?
q: why?
ans: undefined behaviour.
to explain, if try modify const
variable value accessing through non-const
pointer, invokes undefined behaviour.
as per c11
standard, chapter 6.7.3, paragraph 6.
if attempt made modify object defined const-qualified type through use of lvalue non-const-qualified type, behavior undefined.
note: recommended signature of main()
int main(void)
.
Comments
Post a Comment