title: a static variable in c
body: <p>Hi
  I'm studying for my test in C and i've encountered a question which i can't figure its answer.  </p>
  
  <p>A programmer wrote a program to count number of users (Count.h, Count.c):</p>
  
  <pre><code>/******** FILE: Counter.h ***********/
  static int counter = 0;
  int getUsersNum ();
  /******** END OF FILE: Counter.h ****/
  
  /******** FILE: Counter.c ***********/
  #include "Counter.h"
  int getUsersNum ()
  {
      return counter;
  }
  /******** END OF FILE: Counter.c ****/
  </code></pre>
  
  <p>And a tester to test it:</p>
  
  <pre><code>/******** FILE: CounterMain.c ***********/
  #include "Counter.h"
  #include &lt;stdio.h&gt;
  int main ()
  {
      int i;
      for (i=0;i&lt;5;++i)
      {
          ++counter;
          printf ("Users num:  %d\n", getUsersNum());
      }
      return 0;
  }
  /******** END OF FILE: CounterMain.c ****/
  </code></pre>
  
  <p>Suprisingly the output was:</p>
  
  <pre><code>Users num: 0
  Users num: 0
  Users num: 0
  Users num: 0
  Users num: 0
  </code></pre>
  
  <p>I can't see why with this use of static variable the counter does not advance.. why did they get such input?</p>
  
  <p>thank you all!</p>