2

I want to know that what happens inside the compiler... like whether it store global variable at different location.

3 Answers3

3

The Wikipedia page on symbol tables can provide you a basic understanding.

http://en.wikipedia.org/wiki/Symbol_table

In computer science, a symbol table is a data structure used by a language translator such as a compiler or interpreter, where each identifier in a program's source code is associated with information relating to its declaration or appearance in the source, such as its type, scope level and sometimes its location.

[...]

A common implementation technique is to use a hash table implementation. A compiler may use one large symbol table for all symbols or use separated, hierarchical symbol tables for different scopes.

Emphasis mine.

Community
  • 1
  • 1
Pubby
  • 51,882
  • 13
  • 139
  • 180
0

It knows that the variable is global or local by how you declare it.

//declared at namespace scope - global
extern int x;     

int main()
{
   //declared inside a method - local
   int y;
};
Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
0

Generally there are 4 scopes of any variables. Using extern keyword you are making that variable extern explicitly.(by default global variables are extern)

Using staticlimiting the variable or function scope to it's current file

In accordance of that the memory are allocated in different segments

        global: visible in more than one source file
 -- data segement(also differs whether initialised or uninitialized)


        local : visible with in { } it also called function(){} scope
 -- on stack 


        block : {} inside any function another scope of block valiables with in {}

     -- on stack if with in function 


        file : making static variable is limited to it's file scope or 
    current translation unit. -- again data section
Omkant
  • 9,018
  • 8
  • 39
  • 59