title: static and extern global variables in C and C++
body: <p>I made 2 projects, the first one in C and the second one in C++, both work with same behavior.</p>
  
  <p>C project:</p>
  
  <p><strong>header.h</strong></p>
  
  <pre><code>int varGlobal=7;
  </code></pre>
  
  <p><strong>main.c</strong></p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include "header.h"
  
  void function(int i)
  {
      static int a=0;
      a++;
      int t=i;
      i=varGlobal;
      varGlobal=t;
      printf("Call #%d:\ni=%d\nvarGlobal=%d\n\n",a,i,varGlobal,t);
  }
  
  int main() {
      function(4);
      function(6);
      function(12);
      return 0;
  }
  </code></pre>
  
  <p>C++ project:</p>
  
  <p><strong>header.h</strong></p>
  
  <pre><code>int varGlobal=7;
  </code></pre>
  
  <p><strong>main.cpp</strong></p>
  
  <pre><code>#include &lt;iostream&gt;
  #include "header.h"
  using namespace std;
  
  void function(int i)
  {
      static int a=0;
      int t=i;
      a++;
      i=varGlobal;
      varGlobal=t;
      cout&lt;&lt;"Call #"&lt;&lt;a&lt;&lt;":"&lt;&lt;endl&lt;&lt;"i="&lt;&lt;i&lt;&lt;endl&lt;&lt;"varGlobal="&lt;&lt;varGlobal&lt;&lt;endl&lt;&lt;endl; 
  }
  
  int main() {
      function(4);
      function(6);
      function(12);
      return 0;
  }
  </code></pre>
  
  <p>I read that global variables are <strong>extern</strong> by default and in C and <strong>static</strong> by default in C++; so why does the C++ code works?</p>
  
  <p>I mean <em>int varGlobal=7;</em> is same as <em>static int varGlobal=7;</em> and if it's static then it can be used only in the file it was declared, right?</p>