Search code examples
cstaticname-mangling

Is a static variable name, same as function name allowed in C?


I was working on some C-output questions and found the following code:

http://ideone.com/O0tQnr

In this code , as one can see , inside main , a static variable having same name has been declared. For this I searched on Stack-Overflow and found

How are static variables with the same name in different functions identified by the System?

The answers given for this question suggest different approaches viz.

  1. The names of static variables are usually included within the debugging symbol table.
  2. some embedded ones (compilers) simply add a number to the end of each duplicate name
  3. They are likely mangled in the table.

I wanted to know how are static variables actually implemented in C, as all answers are suggesting something different?

Also to check whether this was only a one-off chance I also ran the code:

http://ideone.com/zpRVCa

but the error:

prog.c:5:21: error: called object ‘GetSize’ is not a function or function pointer
int myvar=GetSize(GetSize);
                 ^
prog.c:4:11: note: declared here
static GetSize;
       ^

indicates that the compiler found a conflicting declaration / redeclared Getsize.


Solution

  • Different entities may have the same identifier if they have different scopes or are in different name spaces1. In the case of int main() { static int main; … }, the first main has file scope, and the second main has block scope.

    At any particular point, only one identifier is visible in a name space. In GetSize(GetSize), only the static GetSize is visible. It hides the int GetSize(int), so the function GetSize is not visible. Thus, this code gets an error.

    An identifier declared at file scope with static has internal linkage. An object identifier declared at block scope without extern (including those that have static) has no linkage. Because these identifiers do not have external linkage, they never need to be known outside the current translation unit. Therefore, these identifiers do not need to appear in object files: There is no need for them to have names visible to the linker. They are typically accessed by code generated by the compiler during compilation of the translation unit, and this code addresses objects numerically (by location in memory), not by name.

    Many C implementations provide debugging facilities. Debuggers generally need to know the names of things even if they have internal or no linkage. In these cases, the C implementation may use any scheme it desires to record information about names.


    1 The name spaces of C are: label names; tags of structures, unions and enumerations; members of structures or unions (a separate space for each structure or union); and all other identifiers. The standard also refers to a name space for macro names.