title: C Global and Static variable storing in memory
body: <p>From what I have learned about global and static variables,
  If a C variable is declared outside all functions in a source file as:  </p>
  
  <pre><code>int a;  
  </code></pre>
  
  <p>This variable can be accessed by other files once it has an extern declaration for it in that file.<br>
  But if the same variable is declared as:  </p>
  
  <pre><code>static int a;
  </code></pre>
  
  <p>then this variable can be used only by the current file, any other file wont be able to see this variable.</p>
  
  <ol>
  <li><p>When the program is loaded into the memory at run time, both Global and static variable are present in the Data section of this program.<br>
  I want to understand that as both are stored in the same memory segment, how is the static variable protected from not getting used in any instruction out of its scope.<br>
  What I think is that the scope of the variable and its access will be taken care of by the compiler. Please comment if I am wrong and add if I am missing any detail.</p></li>
  <li><p>Regarding Extern variable.
  If,</p>
  
  <pre><code>int a;  
  </code></pre>
  
  <p>is defined in file file1.c and is declared in file file2.c as:  </p>
  
  <pre><code>extern int a;  
  </code></pre>
  
  <p>both files belongs to different processes, let it be process1 and process2 respectively.
  So when process1 is running and its address space is loaded in the memory its data section variable "a" will be available.<br>
  I have a doubt here, that is, when process2 is running will this variable also be loaded in process2's data section? or how it is managed.</p></li>
  </ol>
  
  <p>Please help me clear my above mentioned doubts. 
  I have searched on the web and read a few articles and need to confirm if I have understood is correctly.<br>
  Aso, please do suggest me a good article or book which will help me understand the above concepts clearly.</p>