I was reading this thread (which discusses how you can't declare global variables in header files, etc) and thought I'd try it out. I seem to remember reading something about how const global variables in C++ were automatically static, that is, limited to the scope of one file, which would explain Elysia's last post.

Evidently I remembered correctly on that point.

Anyway. I came across a very strange phenomenon in my experimenting, which I eventually narrowed down to this:
Code:
$ ./globalx
$ cat globalx.c
int x;

$ cat globalxmain.c
int x;

int main() {
    return 0;
}
$ gcc -c globalxmain.c
$ gcc -c globalx.c
$ gcc globalx.o globalxmain.o -o globalx
$ ./globalx
$
Why does this work? How can a global variable, declared in two separate source files, be legal C? (It's not legal C++, by the way.)

I thought global variables could only be declared once, like functions, or the linker would complain . . . .

What am I missing here?

[edit] Mm'kay, if I initialize x in both places I get the expected linker error.
Code:
$ gcc globalx.c globalxmain.c -o globalx
/tmp/ccvX2E7d.o:(.data+0x0): multiple definition of `x'
/tmp/ccMaJSet.o:(.data+0x0): first defined here
collect2: ld returned 1 exit status
$
I guess un-initialized global variable declarations can be repeated or something like that? . . . . [/edit]