Originally Posted by
MartinR
Well I think it is obvious - How can it be accessed by other file if it has file scope in the same time?
Consider this program consisting of two source files:
Code:
/* main.c */
#include <stdio.h>
void foo(void);
void bar(void)
{
extern int i;
printf("i=%d\n", i);
}
void baz(void)
{
printf("i is equal to %d\n", i);
}
int main(void)
{
foo();
bar();
baz();
}
Code:
/* foo.c */
int i;
void foo(void)
{
i = 123;
}
If you compiled the above program, you would find that the compiler calls you out on an error in main.c concerning the baz function, i.e., it would complain about i not being declared or something like that.
The reason is that the name i has file scope in foo.c, but in main.c its scope is that of the bar function, hence it is out of scope in the baz function. Yet, because i has external linkage, the i that was declared extern in bar refers to the same int object as the i that was declared at file scope in foo.c