title: Understanding how extern works
body: <p>The way I understand extern is that we are able to declare a variable anywhere in  a program and use it, but we can just define it once. I am getting an error in the following program.</p>
  
  <p>hello.c</p>
  
  <pre><code>#include &lt;stdio.h&gt; 
  #include "function.h"
  extern int c;
  int main() 
  { 
      int c;
  
  
      c=10;
      printf("%d\n",c);
      printExternValue();
  
      return 0;
  
  }
  </code></pre>
  
  <p>function .h</p>
  
  <pre><code>void printExternValue();
  </code></pre>
  
  <p>function .c</p>
  
  <pre><code>#include "function.h"
  #include "stdio.h"
  extern int c;
  void printExternValue()
  {
      printf("%d\n",c);
  }
  </code></pre>
  
  <p>I expect this program to print out:</p>
  
  <p>10<br>
  10</p>
  
  <p>But it's not doing so since it's giving an error. I re-declared the variable c in the function.c file with the intention of using the value that is stored in the so called external storage.</p>
  
  <p>Error: function.c:(.text+0x6): undefined reference to `c'</p>
  
  <p>I am currently reading a PDF file from tutorialspoints which I think to be very redundant since the intention of creating a variable with the aggregate extern is useless. The right way this should be done is that they define the variables outside the function is that right?</p>
  
  <pre><code>#include &lt;stdio.h&gt; 
  
  // Variable declaration: 
  extern int a, b; 
  extern int c; 
  extern float f; 
  
  int main () 
  { 
    /* variable definition: */ 
    int a, b; 
    int c; 
    float f; 
  
    /* actual initialization */ 
    a = 10; 
    b = 20; 
  
    c = a + b; 
    printf("value of c : %d \n", c); 
  
    f = 70.0/3.0; 
    printf("value of f : %f \n", f); 
  
    return 0; 
  } 
  </code></pre>