include
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?
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 variablei(again, this is C, not C++).GCC’s
-Wshadowcompiler flag would warn you about the localishadowing the global one.