What is the difference between declaring static variable inside a block and outside a block in a file? Eg, here, what is difference between static variables a,b,c,d? Can we declare a static variable that is accessible from all files of a program?
static int a;
void getVol(..)
{
static int b;
}
int main()
{
static int c;
while(condition)
{
static int d;
....
}
some code here;
return 0;
}
Static variable inside a block(local static variable) -
Static variable outside a block(Global static variable) -
means the variable
a
is a file scope variable, i.e, it can't be seen from other files.means the local variable
b
has a life cycle that goes from the program starts to the program ends, i.e, you can assign it some value, while on the next call of the function, it remembers that value.c
andd
are similar tob
.The following worked for me:
Ultimately, there is no difference. Ignoring (for the moment) static member functions,
static
means what it means -- but we see different parts of what it means under different conditions because some of what it means can also happen without the keyword.When you use the
static
keyword, the object being defined always has:Both of these are true about a static variable whether defined inside or outside a block. One part or the other will happen by default, even if you don't use the
static
keyword, but if you use the keyword, you always get both.static
member functions are analogous, but since they're functions they don't exactly have lifetime -- all functions have static lifetime. A static member function has local visibility (i.e., its name is visible only with its class) and something sort of like "static lifetime" -- the function isn't bound to an instance of the class.Edit: for those who care about the specific time at which a block-level static variable is intialized, the gory details are as follows (§6.7/4):
So, the variable will be zero-initialized very early in the startup of the program. Then, if other initialization has been specified, that will happen no later than when execution passes through the initialization (but could happen earlier than that). Note, however, the difference between constant initialization and other initialization. Just for example, consider something like this:
Here,
x
is constant initialized, buty
is not. Sincex
is constant initialized, it is initialized upon entry to the block, so when we print out its value, we should get1
.y
, however, is not constant initialized, and the goto means that execution never flows through its initialization -- therefore, it will retain the0
that it was initialized with before any other initialization took place, so (with a properly functioning compiler) the output will be:File scoped variables act exactly like global variables, except their use is restricted to the file in which they are declared (which means you can not extern them to other files). A fixed duration variable is one that retains it’s value even after the scope in which it has been created has been exited! Fixed duration variables are only created (and initialized) once, and then they are persisted throughout the life of the program. Check this link:http://www.learncpp.com/cpp-tutorial/43-file-scope-and-the-static-keyword/
Using the static keyword in block scope is exactly like a global variable with respect to where it is stored, however the difference is that it is only accessible within the block scope that it was declared. Recall that as you nest blocks the inner-most block takes precedence when referencing an identifier -- which the same applies to static variables.
Consider the following snippet of code to illustrate my response: