title: Access of static variable from one file to another file
body: <p>I recently came across the question like how to access a variable which declared static in file1.c to another file2.c?</p>
  
  <p>Is it possible to access static variable?</p>
  
  <p>My understanding about static keyword in C is,</p>
  
  <p><code>static</code> is "internal linkage", so they are accessible only from one compilation unit - the one where they were defined. Objects declared with internal linkage are private to single module.</p>
  
  <p>As one of my friend suggest me below solution.</p>
  
  <p>In <code>file1.c</code></p>
  
  <pre><code>   #include &lt;stdio.h&gt;
  
     int main()
     {
            int b=foo();
            printf("%d",b);
            return 0;
     }
  </code></pre>
  
  <p>in <code>file2.c</code></p>
  
  <pre><code>   static int a=25;
  
     int foo()
     {
          return a;
     }
  </code></pre>
  
  <p>compiled by <code>gcc file1.c file2.c -o file</code></p>
  
  <p>If I do above I can access the variable.</p>
  
  <p>So my questions are:</p>
  
  <ol>
  <li><p>Does the above program violate static variable rules?</p></li>
  <li><p>If not, why is this so, and is there any other way to access static variable except including file (<code>#include &lt;…&gt;</code>) not like this.</p>
  
  <p><a href="https://stackoverflow.com/questions/2311249/how-am-i-able-to-access-a-static-variable-from-another-file">How am I able to access a static variable from another file?</a></p>
  
  <p><a href="https://stackoverflow.com/questions/1046546/in-c-how-do-i-restrict-the-scope-of-a-global-variable-to-the-file-in-which-its">In C, how do I restrict the scope of a global variable to the file in which it&#39;s declared?</a></p></li>
  <li><p>Correct me if I'm wrong with static variable concept and if any better solutions are available to access static variable?</p></li>
  </ol>