GeekInterview.com
  I am new, Sign me up!
 
GeekInterview.com  >  Interview Questions  >  Programming  >  C++
Go To First  |  Previous Question  |  Next Question 
 C++  |  Question 129 of 203    Print  
What is the difference between static global variable and static local variable?

  
Total Answers and Comments: 6 Last Update: July 31, 2009     Asked by: adil khan 
  
 Sponsored Links

 
 Best Rated Answer
Submitted by: wow_suraj
 
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.

Above answer was rated as good by the following members:
yzesong
May 08, 2007 02:51:44   #1  
Harikrishna        

RE: What is the difference between static global varia...
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.

 
Is this answer useful? Yes | No
October 01, 2007 01:06:42   #2  
paresh        

RE: What is the difference between static global varia...
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.
 
Is this answer useful? Yes | No
April 22, 2008 22:17:46   #3  
vshylaja Member Since: April 2008   Contribution: 10    

RE: What is the difference between static global variable and static local variable?
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.

 
Is this answer useful? Yes | No
May 02, 2008 08:09:53   #4  
wow_suraj Member Since: May 2008   Contribution: 22    

RE: What is the difference between static global variable and static local variable?
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.

 
Is this answer useful? Yes | NoAnswer is useful 1   Answer is not useful 0Overall Rating: +1    
December 07, 2008 10:19:43   #5  
burraganesh Member Since: December 2008   Contribution: 20    

RE: What is the difference between static global variable and static local variable?
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.

 
Is this answer useful? Yes | No
July 30, 2009 22:27:10   #6  
yzesong Member Since: July 2009   Contribution: 20    

RE: What is the difference between static global variable and static local variable?
They have almost same lifetime(local is shorter) but different access scope.
 
Is this answer useful? Yes | No


 
Go To Top


 Sponsored Links

 
About Us -  Privacy Policy -  Terms and Conditions -  Contact -  Ask Question -  Propose Category -  Site Updates 

Copyright © 2005 - 2009 GeekInterview.com. All Rights Reserved

Page copy protected against web site content infringement by Copyscape