Assume there is a global variable & a static global
variable.Both gets memory allocated from the data segment.
Then how does the visiblity of static global varible gets
limited to the file alone (in which it gets declared) ???

Answer Posted / vinaykumarmp

Static variables are local in scope to their module in which
they are defined, but life is throughout the program. Say
for a static variable inside a function cannot be called
from outside the function (because it's not in scope) but is
alive and exists in memory. The next time this function is
entered (within the same program) the same chunk of memory
would be accessed now retaining the variables old value and
no new memory is allocated this time for this variable like
other variables in the function (automatic variables). So
basically the variable persists throughout the program.
Similarly if a static variable is defined in a global space
(say at beginning of file) then this variable will be
accessible only in this file (file scope).

Is This Answer Correct ?    11 Yes 21 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

No New Questions to Answer in this Category !!    You can

Post New Questions

Answer Questions in Different Category