Constant: A variable defined as const tells that the value of the variable can not be changeed.
Local const Variable
Local variable are the variables whose value we can change at different places in the same function or in different function by passing it as argument. But if we attach the word const to it then we can not change the value once declaed.
void myfunc
{
const int a = 30;
int const *p = &a;
a = 30; //giving compilation error error as it is a const variable.
*p = 30 // no compilation error
}
Global const variable
A global constant variable is first thing is global so scope is not just local hence can be accessed anywhere in the program but the major thing here is that global const variable is stored in read only memory so whenever you try to change the value using a pointer u get the segmentation fault as memory is readonly. Check the following example -
const int a = 20; //Global const variable
int main()
{
int const *p = &a; // As p is a pointer to global const variable
*p = 30;//No compilation error but it will give segmentation fault
}