Static variable will be visible in A. Function in which they are defined B. Module in which they are defined C. All the program D. None

Showing Answers 1 - 7 of 7 Answers

Mahesh

  • Oct 3rd, 2005
 

Answer is A. Function in which they are defined
Bcos the scope of static variable is local to the block in which the variable is defined and life of the variable persists between different function calls. 

  Was this answer useful?  Yes

Ranjitha

  • Jan 31st, 2007
 

Ans : B

Because the lifetime of the static variable is the entire module in which they are used.

  Was this answer useful?  Yes

Static and Global variable differ a lot in their behaviour to life and scope. At first, let me distinguish between life and scope. Life of an object determines whether the object is still in the memory (of the process) whereas scope of the object is whether can I know the variable by its name at this position. It is possible that object is live, but not visible (not in scope) but not that object is not alive but in scope (except for dynamically allocated objects where you refer object thro pointers). 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 its 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). On the other hand global variables have to be defined globally, persists (life is) throughout the program, scope is also throughout the program. This means such variables can be accessed from any function, any file of the program. So if you have a global variable and you are distributing your files as a library and you want others to not access your global variable, you may make it static by just prefixing keyword static (ofcourse if same variable is not required in other files of yours).

So the answer is B. Module in which they are defined

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions