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;
}
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.
For those who care about the specific time at which a block-level static variable is initialized, the gory details are as follows (§6.7/4):
The zero-initialization (8.5) of all block-scope variables with static storage duration (3.7.1) or thread storage duration (3.7.2) is performed before any other initialization takes place. Constant initialization (3.6.2) of a block-scope entity with static storage duration, if applicable, is performed before its block is first entered.
An implementation is permitted to perform early initialization of other block-scope variables with static or thread storage duration under the same conditions that an implementation is permitted to statically initialize a variable with static or thread storage duration in namespace scope (3.6.2). Otherwise such a variable is initialized the first time control passes through its declaration; such a variable is considered initialized upon the completion of its initialization.
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:
int g() { return 2; }
int f() {
goto bypass;
static int x = 1;
static int y = g();
bypass:
std::cout << x << "\n" << y;
}
Here, x
is constant initialized, but y
is not. Since x
is constant initialized, it is initialized upon entry to the block, so when we print out its value, we should get 1
. y
, however, is not constant initialized, and the goto means that execution never flows through its initialization -- therefore, it will retain the 0
that it was initialized with before any other initialization took place, so (with a properly functioning compiler) the output will be:
1
0
Static variable inside a block(local static variable) -
Static variable outside a block(Global static variable) -
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With