1
#include<stdio.h>
static int a=5;
main()
{
     static int a=15;
     printf("%d\n",a);
}

So, how are both variables a stored in internal memory?

How are global and local variables with the same variable names stored internally in memory?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278

4 Answers4

1
#include<stdio.h>
static int a=5;
int main()
{
    printf("%p\n",(void *)&a);
    static int a=15;
    printf("%p\n",(void *)&a);
    return 0;
}

Output for the upper program is

0x564e6b67a030
0x564e6b67a034

So you can see that both are stored in different addresses. As one is a global variable and other is local.

yajiv
  • 2,901
  • 2
  • 15
  • 25
1

The names are only of interest to the human reader and the compiler/linker translating that code to machine executable code. The final object code resolves these to addresses and the names no longer exist.

The compiler distinguishes these the same way you do - by scope; when two identical symbols in the same namespace are in scope simultaneously, the symbol with the most restrictive scope is visible (i.e. may be accessed via the name).

For symbols with external linkage (in your example there are none other then main), the compiler retains the symbol name in order to resolve links between separately compiled modules. In the fully linked executable the symbol names cease to exist (except in debug build symbol meta-data).

Clifford
  • 88,407
  • 13
  • 85
  • 165
  • @SudeepPatel : No - not at all. The variable `a` has _internal_ linkage due to the `static` _linkage specifier_. Removing the `static` qualifier will cause it to have _external_ linkage - the `extern` keyword is not necessary, but would not cause an error (likely to cause a warning because it is an unusual and unnecessary use of `extern`). It cannot of course be both `static` and `extern` since they specify opposing linkage. Since you did not say what the error was I am left guessing. – Clifford Mar 03 '18 at 14:19
0

The facetious answer is that they are stored in different places.

Remember that the names of variables do not (normally) form part of the compiled program, so the compiler just follows the normal rules of variable shadowing. So in your case your print function (that's not a standard C function by the way - did you mean printf?) outputs the a declared in main. The fact that you've used the same name will not bother the compiler at all.

Finally C provides no way of accessing the global scoped a once the other declaration is encountered in main as it's static. (It is wasn't static you could use extern.) See How can I access a shadowed global variable in C?

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
0

The thing is the scope don't let them mess up. The first one has file scope and the other has block scope. (They are different variables - they are stored in separate memories.)

When you use it in the block - compiler checks whether this reference is resolved by anything in the same block. It gets one. And done.

And in case it is in some other function - if it doesn't find anything named a - the search ends in file scope where it finds the name a. That is where the story ends.

Both being static their storage duration is same. They live till the program exists. But their scope is different. If the scope was same too - compiler would have shown you error message.

Here if you compile with -Wshadow option - it will warn you about shadowing a variable. You shadowed the outer a with the inner on that block. That's it.

user2736738
  • 30,591
  • 5
  • 42
  • 56