tags:

views:

52

answers:

2

Hi,

#include <stdio.h>
static int i = 10;

int
main()
{
   static int i = 20;

   printf ( "i = %d\n", i );

   return 0;
}

There are two static variables, one in global scope and one in function scope. The compiler is not throwing "multiple definition" error. Could you please let me know where the two static vars are stored?

+2  A: 

The two variables are stored separately because they are distinct - it is the compiler's problem to ensure that they are separate.

The variables are both initialized before the program starts - this is C, not C++, where the rules are slightly different.

Inside main() as shown, you cannot access the global variable i (again, this is C, not C++).

GCC's -Wshadow compiler flag would warn you about the local i shadowing the global one.

Jonathan Leffler
But static variables are stored in initialized data segment, Am i right?If so how compiler differentiates the local i with the global i?
Ganesh Kundapur
However it wants to. That's the implementor's job.
R..
Hint: on a normal implementation, *neither* of these variables will still have a name after compiling, except possibly in data kept for the debugger.
R..
as automatic variables goes to stack. Where the static variables goes to? Where the global static static is stored? Where the local static variable stored?How the compiler differentiates global static from local static?
Ganesh Kundapur
objdump -t ./a.out lists0804a014 l O .data 00000004 i0804a018 l O .data 00000004 i.1705Here one of the static variable is mangled. Does C compiler does name mangling?
Ganesh Kundapur
The variables have different addresses - that is sufficient for the compiler to distinguish between them. Names are a convenience for humans - they're a nuisance to the average compiler. And yes, the C compiler sometimes does name mangling - that used to be how static variables were hidden (as a public name with a quasi-random suffix).
Jonathan Leffler
They go to the .data segment just like any initialized variable. They just don't (usually) get an exported symbol. If they do get a symbol, it will be decorated in a way that makes hard/impossible to refer to them from any C code outside their proper scope.
RBerteig
From the symbols, its known that C compiler does name mangling. If it does name mangling for variables, why function name mangling is not supported?
Ganesh Kundapur
@Ganesh: C static function names might be mangled - the same as static variables might be mangled.
Jonathan Leffler
A: 

These variables are called "symbols", and during compiling a table is generated, the "symbol table". This table contains the name, type, scope and memory pointer to each symbol (this is like the minimum, you usually have a bunch of more stuff), and each time a reference is made to an symbol in a specific scope, it's substituted for an index into the table. These indices are unique, so is the combination of name+scope.

So in short, the names of the variables are simply decoration, internally the compiler works with a symbol table and indices into it. Statics are initialized during program startup by iterating through a table of pointers to them and putting the correct values in place.

Tobias