title: Access variable that was overwritten within given scope via function that uses global variables
body: <p>I have this piece of code:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  
  // global variable definition
  int x = 0;
  
  void access_global(){
      cout &lt;&lt; "ACCESSING FROM access_global()" &lt;&lt; endl;
      if (x &gt; 0){
          cout &lt;&lt; "LOCAL VARIABLE" &lt;&lt; endl;
          cout &lt;&lt; x &lt;&lt; endl;
      }
      else{
          cout &lt;&lt; "GLOBAL VARIABLE" &lt;&lt; endl;
          cout &lt;&lt; x &lt;&lt; endl;
      }
  }
  
  
  int main(int, char*[])
  {
      // declare new variable in given scope with same name as global variable
      int x = 1;
  
      cout &lt;&lt; "ACCESSING FROM main()" &lt;&lt; endl;
      if (x &gt; 0){
          cout &lt;&lt; "LOCAL VARIABLE" &lt;&lt; endl;
          cout &lt;&lt; x &lt;&lt; endl;
      }
      else{
          cout &lt;&lt; "GLOBAL VARIABLE" &lt;&lt; endl;
          cout &lt;&lt; x &lt;&lt; endl;
      }
  
      access_global();
  
      return 0;
  }
  </code></pre>
  
  <p>It outputs:</p>
  
  <pre><code>ACCESSING FROM main()
  LOCAL VARIABLE
  1
  ACCESSING FROM access_global()
  GLOBAL VARIABLE
  0
  </code></pre>
  
  <p>Why is <code>access_global()</code> not accessing <code>x</code> in the <code>main()</code> scope?
  Is  it possible to modify <code>access_global()</code> function that it will show primary <code>x</code> variables from <code>main()</code> scope and if they are not defined then show those defined outside of <code>main()</code>? If is not possible can you please explain why? Thanks</p>