title: Difference between static global variable and static variable inside a function
body: <p>Lets say I have a function <code>void func()</code> and I use the variable <code>testVar</code> inside of this function. Additionally I need that the variable persists after leaving the function. Usually I would do it by using a static variable inside of the function(1). But what are the differences, when I would use a global variable instead(2)?</p>
  
  <pre><code>static int testVar = 0; //global variable instead(2)
  
  void func()
  {
      static int testVar = 0; //static variable inside of the function(1)
  
      if(testVar++){
          //do something
      }   
  }
  </code></pre>
  
  <p>What does the compiler do in both cases in detail? Is there a scenario where I should use method(2) provided the variable is only needed in <code>func()</code>?</p>