title: C - extern, static, include
body: <p>Scoping in C is confusing as hell. I have a variable: "int qwe". This var should be visible in one or more files - f1.c in this case, but not the another f2.c .</p>
  
  <p>Say i have: main.c, f1.c, f2.c, header.h</p>
  
  <p>main:
  call f1();
  call f2();</p>
  
  <p>header:</p>
  
  <pre><code>#ifndef HEADER_INCLUDED
  #define HEADER_INCLUDED
  
  int qwe = 1;
  void f1();
  void f2();
  #endif // HEADER_INCLUDED
  </code></pre>
  
  <p>f1.c:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  
  extern int qwe;
  
  void f1(){
      printf("In f1: %d\n", qwe);
  }
  </code></pre>
  
  <p>f2.c:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  
  static int qwe = 2;
  
  void f2(){
      printf("In f2: %d\n", qwe);
  }
  </code></pre>
  
  <p>Now this gets confusing. There is definition and declaration. I have defined qwe in the header, declared it in f1.c. Is that correct? Should definition happen in header and declaration in f1.c instead? I tried the latter case, but got an error - "multiple definition of qwe". When i removed the #include directive from f1.c, it worked... It also works when i remove the extern keyword. Is extern redundant?</p>
  
  <p>f2.c i guess it's ok and behaves as expected, or is it? But if i put an #include with header, it breaks. Why is that?</p>
  
  <p>When should i use #include in source files? If i don't include it f1.c or f2.c it works...</p>
  
  <p>Also, if i define a variable as static inside a function, like static int i = 0; This variable will not be destroyed when function exist, it will keep it's memory and data. And next time that same function gets called, will have access to it, right? But the var won't be reinitialized to 0, i.e. the line where is defines won't execute. Correct?</p>
  
  <p>Life stomps me :(</p>