title: Why can I call a function from another file (with warning) but cannot use a variable from another file without declaring it?
body: <pre class="lang-c prettyprint-override"><code>// a.c 
  #include &lt;stdio.h&gt;
  
  int main() {
      printf(&quot;%d&quot;, sum());
  }
  </code></pre>
  <pre class="lang-c prettyprint-override"><code>// b.c 
  int sum() {
     return 10;
  }
  </code></pre>
  <p>I compile those two files with
  <code>gcc a.c b.c</code> normally, it works fine with an <code>implicit declaration</code> warning and this warning disappears if I declare <code>sum</code> in <code>a.c</code>. I've read in another post that it is fine if I don't declare the function and if I call it properly, but it's not advised to do so and I should always declare what I use in each file, and this is fine and understandable.</p>
  <p>What I don't understand is why there's a distinction between functions defined in another file and variables. Why can't I use a variable defined in another file in the same manner as above? I can also get it to run by declaring <code>i</code> in the file where I use it, though.</p>
  <pre class="lang-c prettyprint-override"><code>// c.c 
  #include &lt;stdio.h&gt;
  int main() {
      printf(&quot;%d&quot;, i);
      return ;
  }
  </code></pre>
  <pre class="lang-c prettyprint-override"><code>// d.c 
  int i = 12;
  </code></pre>
  <p>compiled with <code>gcc c.c d.c</code>, produces the following error</p>
  <pre><code>c.c: In function ‘main’:
  c.c:3:19: error: ‘i’ undeclared (first use in this function)
        printf(&quot;%d&quot;, i);
                     ^
  c.c:3:19: note: each undeclared identifier is reported only once for each function it appears in
  </code></pre>