What is the difference between static global variable and static local variable?

Questions by adil khan

Showing Answers 1 - 18 of 18 Answers

Harikrishna

  • May 8th, 2007
 

Local static variables are the ones whose scope is local but they are placed in the data area which also stores the other global/static variables.

Now local static variables name are mangled in such a way that their name includes the function name in which they are defined.

In case of global static variable mangling scheme include the file name in the name of the variable and so this is the reason we are not able to extern those variables in the other source file.

I hope the answer is clear that although all the static/global variables are stored in the data section, it is only through their name (mangled name)compiler differentiates between them.

paresh

  • Oct 1st, 2007
 

There is no such thing as global & local. Only one copy of static variable is created & it is global by default you cannot change it.

  Was this answer useful?  Yes

The difference lies in the scope (i.e where they can be used), even though they both have the same lifetime.
Static local variables are local to the method/function where they are defined and can be used within that function only.
Static global variables have file level scope and can be used by any functions within the same file.

wow_suraj

  • May 2nd, 2008
 

The difference between both the type of variable is of the scope of the variable.

Static local variable is initialized at the time of fucntion call and some memory is assigned for the variable. But as the function return back to it's call the scope fo variable ends and also the memory occupied by the variable is released. While Static global variable has it's scope for the whole of programme life.

For both these the default initial value is 0
A static global variable can only be accessed in the file it exists, we cannot use it in other files, even after mentioning it as extern
In static local variable, the life is extended (i.e. active in memory) between function calls, but the scope is limited to the function in which it is defined. Thus a static local variable acts as a semi-global variable.

  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