title: static member variable file scope
body: <p>Static variable has the scope inside that file only where they are been declared, as shown in below code:</p>
  
  <p>file1-</p>
  
  <pre><code>static int a;
  </code></pre>
  
  <p>file2-</p>
  
  <pre><code>extern int a;
  </code></pre>
  
  <p>This will give linking error as static variable a has the scope in file1 only.  But I am confused with below code:</p>
  
  <p>file2-</p>
  
  <pre><code>#include "file1"
  extern int a;
  </code></pre>
  
  <p>Here it would not give any linking error. Then it means compiler is refering "a" which is declared in file1. But when you debug you will find address of variable "a" is different in file1 and file2. Is the compiler creating a another global variable "a" in file2? </p>
  
  <p>complete code-</p>
  
  <p>file temp1.h - </p>
  
  <pre><code>static int w = 9;
  
  class temp1 
  {
  public:
     temp1(void);
  public:
     ~temp1(void);
  
     void func();
  
  };
  </code></pre>
  
  <p>........................cpp.................</p>
  
  <pre><code>temp1::temp1(void)
  {
     int e =w;
  }
  
  temp1::~temp1(void)
  {
  }
  void temp1::func()
  {
  }
  </code></pre>
  
  <p>.......................................
  file2-</p>
  
  <pre><code>#include "temp1.h"
  
  extern int w;
  int _tmain(int argc, _TCHAR* argv[])
  {
     w  = 12;
  
     temp1 obj;
     return 0;
  }
  </code></pre>
  
  <p>here when i debug and check the value and adrress in temp1 constructor and in file2 is different.</p>