title: how to define a variable in one file and use it in another one
body: <p>The question may be very basic but I am not getting clue anyways ...</p>
  
  <p>I have two files ...
  and I am mentioning what I want to do .</p>
  
  <p><strong>file 1</strong></p>
  
  <pre><code>...
  j = data-&gt;alloc_len;
  ...
  </code></pre>
  
  <p><strong>file 2</strong></p>
  
  <pre><code>...
    for(i=0;i&lt;j;i++)
  ...
  </code></pre>
  
  <p>Its clear from above I want to assign value to a variable in one file and want to use that value in other file. </p>
  
  <p>I tried <code>#include "file1.c"</code> in file2.c but it is giving lot of re-declaration errors.</p>
  
  <p>I tried creating a seperate header file which only have one line <code>int j</code> and then included it in both files using <code>extern</code> but no benefit again.Although I think header files are meant for where i can create and assign a value to a variable in one file and then this value can be propogated to all other files by including this one.   </p>
  
  <p>May be I am wrong but I need it soon so please help me ...Thnx in advance.</p>
  
  <p><strong>Limitation -</strong></p>
  
  <p>The value can be assigned only through <code>file1.c</code> because <code>data</code> structure is declared and defined here only.I can not provide a value to <code>variable j</code> in a header file .</p>
  
  <p><strong>EDIT :</strong></p>
  
  <p>Although I mentioned but I think I could not clear my question.I have tried using it header files.</p>
  
  <p>For debugging purpose I tried this ..</p>
  
  <p><strong>sample.h</strong></p>
  
  <pre><code> extern int j;
  </code></pre>
  
  <p><strong>file1.c</strong></p>
  
  <pre><code> #include "sample.h"
   int j=245;
  </code></pre>
  
  <p><strong>file2.c</strong></p>
  
  <pre><code>  #include "sample.h"
    printf("%d",j);
  </code></pre>
  
  <p>but its getting error .</p>
  
  <p>Error I am getting is :</p>
  
  <pre><code>    Couldn't open module sample.so, tried:
  sample.so: cannot open shared object file: No such file or directory
  ./sample.so.so: cannot open shared object file: No such file or directory
  /usr/local/lib/sendip/sample.so.so: 
      cannot open shared object file: No such file or     
      directory
  /usr/local/lib/sendip/sample.so: undefined symbol: j
  </code></pre>
  
  <p>*<em>none of the file contains main function actually *</em></p>
  
  <p>Actually it is a very large project and I am using <code>Makefile</code> and all files will be linked at run time.</p>
  
  <p>In short,the execution could be understood as there is a <code>main.c</code> file which contains <code>main</code> which in turns call <code>file1.c</code> and which in turn calls <code>file2.c</code> </p>
  
  <p>About descriptive names I would say they are just for showing here otherwise I am already using descriptive name.</p>