When a variable is declared as const it means that , variable is read-only ,and cant be changed .so in order to make a variable read only it should be initialized at the time it is declared.
For better understanding of variables have a look at following program
Every process consists of basically 4 portions of address space that are accessible to the process when it is running
Text - This portion contains the actual m/c instructions to be executed. On many Operating Systems this is set to read only, so that the process can't modify its instructions. This allows multiple instances of the program to share the single copy of the text.
Data - This portion contains the program's data part. It furthere divided into
1) Initialized Read Only Data - This contains the data elements that are initialized by the program and they are read only during the execution of the process.
2) Initialized Read Write Data - This contains the data elements that are initialized by the program and will be modified in the course of process execution.
3)Uninitalized Data - This contains the elements are not initialized by the program and are set 0 before the processes executes. These can also be modified and referred as BSS(Block Started Symbol). The adv of such elements are, system doesn't have to allocate space in the program file for this area, b'coz it is initialized to 0 by OS before the process begins to execute.
Stack - This portion is used for local variables, stack frames
Heap - This portion contains the dynamically allocated memory
int abc = 1; ----> Initialized Read-Write Data
char *str; ----> BSS
const int i = 10; -----> Initialized Read-Only Data
main()
{
int ii,a=1,b=2,c; -----> Local Variables on
Stack
char *ptr;
ptr = malloc(4); ------> Allocated Memory in Heap
c= a+b; ------> Text
}
Data, store data Text, store code
There are 3 (main?) segments/sections of the file produced by a linker. text - program text (and apparently const char arrays. maybe other 'const' arrays, since those can not be changed anyway). I am not 100% sure about the array part, maybe someone will correct me.
data - initialized global data. see examples below. bss - uninitialized global data. Here are some examples
int x = 1; /* goes into data */
int y; /* goes into bss */
const int z = 1;/* goes into text */
this, we've seen go into 'text', since can't be changed anyway,but can be protected