I have a choice to between declaring a variable static or global.
I want to use the variable in one function to maintain counter.
for example
void count()
{
static int a=0;
for(i=0;i<7;i++)
{
a++;
}
}
My other choice is to declare the variable a as global.
I will only use it in this function count().
Which way is the safest solution?
It matters only at compile and link-time. A
staticlocal variable should be stored and initialised in exactly the same way as a global one.Declaring a local
staticvariable only affects its visibility at the language level, making it visible only in the enclosing function, though with a global lifetime.A global variable (or any object in general) not marked
statichas external linkage and the linker will consider the symbol when merging each of the object files.A global variable marked
staticonly has internal linkage within the current translation unit, and the linker will not see such a symbol when merging the individual translation units.