Is it possible to declare multiple static variables of same name in a single C file with different scopes? I wrote a simple programme to check this and in gcc it got compiled and worked fine.
Code:
static int sVar = 44;
void myPrint2()
{
printf("sVar = %d\n", sVar++);
}
void myPrint()
{
static int sVar =88;
printf("sVar = %d\n", sVar++);
}
int main(void)
{
static int sVar = 55;
int i = 0;
for (i = 0; i < 5; i++)
myPrint();
printf("sVar = %d\n", sVar);
myPrint2();
return(0);
}
Now my question is since all "static" variable will reside in same section (.data) then how we can have multiple variables with same name in one section?
I used objdump
to check the different section and found that all the static variables (sVar
) were in .data section but with different names:
0804960c l O .data 00000004 sVar
08049610 l O .data 00000004 sVar.1785
08049614 l O .data 00000004 sVar.1792
Why is the compiler is changing the name of the variables (since C doesn't support name mangling)?
There is a difference between visibility and extent; also, the
static
keyword has different meanings based on scope.Adding the
static
keyword to the block scope versions ofsVar
(myPrint::sVar and main::sVar) changes their extent (lifetime), but not their visibility; both are visible within their respective functions only, and will shadow or hide the file scope version within their local scope. It indicates that the variables have static extent, meaning the memory for them is allocated at program start and held until the program terminates (as opposed to automatic or local extent, where their lifetime is limited to the scope in which they are defined).Adding the
static
keyword to the file scope version ofsVar
doesn't change its extent (file scope variables have static extent by definition), but it does change its visibility with respect to other translation units; the name is not exported to the linker, so it cannot be accessed by name from another translation unit. It is still visible within the current translation unit, which is why myPrint2 can access it.The static keyword has different meanings when it's global and local, the first
sVar
is just a global variable unavailable to other translation units. And static variablessVar
inmyPrint()
andmain()
are in different scopes, so they are different variables. In the body ofmyPrint()
sVar
refers to the local static (hiding globalsVar
), inmyPrint2()
it refers to the global (it's not hidden by anything local), and inmain()
it refers to the localstatic sVar
, which again hides the global since the moment it was declared.Both function will create different stacks, so static part also will be different .Therefore it is possible.
A function-local static variable is something different than a global static variable.
Since there can be as many function-local statics with the same name as you like (provided they are all in different scopes), the compiler might have to change their names internally (incorporating the function's name or the line number or whatever), so that the linker can tell them apart.
Your first
sVar
is global to file, second one is local to the functionmyPrint()
and third one is local tomain()
Function level static variables are not accessible outside the function - they just retain their values on subsequent calls. I assume you've already noted this when you ran your code.
Block scoping. Read K & R.