Scenerio 1: When the const
variable is declared inside main()
, i.e., made local
#include <stdio.h>
#include <conio.h>
main()
{
const int a = 45;
* ((int*)&a)=50;
printf("%d\n",a);
getch();
}
Output MVC++:
a=50
Scenerio 2: When the const
variable is declare outside main()
, i.e., made global
#include <stdio.h>
#include <conio.h>
const int a = 45;
main()
{
* ((int*)&a)=50;
printf("%d\n",a);
getch();
}
Output MVC++:
Unhandled exception...Acess violation.
I understand why I get an error when trying to modify a globally defined const
variable. Why, though, am I not getting an error when I try to modify a const
variable defined locally?