title: C the same global variable defined in different files
body: <p>I am reading <a href="http://coolshell.cn/articles/10115.html#more-10115" rel="noreferrer">this code from here</a>(in Chinese). There is one piece of code about testing global variable in C. The variable <code>a</code> has been defined in the file <code>t.h</code> which has been included twice. In file <code>foo.c</code> defined a <code>struct b</code> with some value and a <code>main</code> function. In <code>main.c</code> file, defined two variables without initialized. </p>
  
  <pre><code>/* t.h */
  #ifndef _H_
  #define _H_
  int a;
  #endif
  
  /* foo.c */
  #include &lt;stdio.h&gt;
  #include "t.h"
  
  struct {
     char a;
     int b;
  } b = { 2, 4 };
  
  int main();
  
  void foo()
  {
      printf("foo:\t(&amp;a)=0x%08x\n\t(&amp;b)=0x%08x\n
          \tsizeof(b)=%d\n\tb.a=%d\n\tb.b=%d\n\tmain:0x%08x\n",
          &amp;a, &amp;b, sizeof b, b.a, b.b, main);
  }
  
  /* main.c */
  #include &lt;stdio.h&gt;
  #include "t.h"
  
  int b;
  int c;
  
  int main()
  {
      foo();
      printf("main:\t(&amp;a)=0x%08x\n\t(&amp;b)=0x%08x\n
          \t(&amp;c)=0x%08x\n\tsize(b)=%d\n\tb=%d\n\tc=%d\n",
          &amp;a, &amp;b, &amp;c, sizeof b, b, c);
      return 0;
  }
  </code></pre>
  
  <p>After using Ubuntu GCC 4.4.3 compiling, the result is like this below:</p>
  
  <pre><code>foo:    (&amp;a)=0x0804a024
      (&amp;b)=0x0804a014
      sizeof(b)=8
      b.a=2
      b.b=4
      main:0x080483e4
  main:   (&amp;a)=0x0804a024
      (&amp;b)=0x0804a014
      (&amp;c)=0x0804a028
      size(b)=4
      b=2
      c=0
  </code></pre>
  
  <p>Variable <code>a</code> and <code>b</code> has the same address in two function, but the size of <code>b</code> has changed. I can't understand how it worked!</p>