Following are the two separate codes written in two separate files Test1.c and Test2.c. I am not using extern
keyword in any file.
//Test1.c
#include <stdio.h>
int a = 1;
int main()
{
printf("test1 - a val = %d\n",a);
fn();
printf("After Return : %d",a);
}
//Test2.c
#include <stdio.h>
int a;
int fn()
{
printf("test2 - a val = %d\n",a);
a++;
}
I compiled this code using gcc:
gcc Test1.c Test2.c
It generates the following output:
test1 - a val = 1
test2 - a val = 1
I tried printing address of variable a
in both codes. The address is also same.
Now I have following questions:
- Does
gcc
automatically compile and link even ifextern
is not used?? Here apparentlygcc
internally does that as I am compiling these two files together. - Is this behaviour with/without
extern
keyword is compiler-dependent?