Your understanding of file scope variables is good.
But your understanding of the C preprocessor is not ;)
To understand what happened here, you should remember that the
#include directives (as well as any other line beginning with #) are processed by the preprocessor, not the compiler. So when you write:
in a C file, the preprocessor will just replace this line by the content of the file
a.h, which means that the compiler has no clue which part of a C file was actually included from a header file.
So in your example, each time you include
a.h, you declare a new, different static variable
i.
To illustrate this, you can try to include
a.h twice in the same file, and you will get a compile error about
i being declared twice.
Or you can try to change the value of
i at the beginning of your
main() function (e.g. by adding
i++ before the call to
a()) and see what happens.