Answers

Question and Answer:

  Home  VxWorks

⟩ Suppose 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)

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).

 167 views

More Questions for you: