I am confused as why the following works:
test.c
#include <stdio.h>
int g;
// ??? should be extern int g; ???
int main(){
printf("%i\n", g);
return 0;
}
lib.c
int g = 3;
Why am I not getting a duplicate symbol error upon compilation? I get the error while trying to do this in C++, so that satisfies me. However, in this example, everything compiles and works (i.e. prints 3 successfully) whether or not I include extern. From reading all the other questions on StackOverflow about extern in C, everyone seems to be saying that extern used on a variable declares the variable but does not define (i.e. allocate memory) for it. But here, if I don't use extern, then I am defining two separate variables, both called g, so there should be some kind of duplicate symbol error. But there isn't, so I am very confused.