When a global variable with the same name is defined across files, only one instance of the variable is actually defined in the memory. For example int temp is defined in a.c, b.c and main.c, when I check the address of the variables, they are same across all the files.
Now the variables are getting extended with out using the specifier extern, so what is the use of the extern?
file a.c
int temp;
a()
{
temp = 10;
printf("the value of temp in a.c is %d\n",temp);
}
file b.c
int temp;
b()
{
temp = 20;
printf("the value of temp in b.c is %d\n",temp);
}
file main.c
int temp;
main()
{
temp = 30;
a();
b();
printf("the value of temp in main.c is %d\n",temp);
}
o/p is
the value of temp in a.c is 10
the value of temp in b.c is 20
the value of temp in main.c is 20.
In the above case the value of temp in main.c is also 20 because it changed in b.c, and the latest value is getting reflected. Why is the variable temp getting extended with out creating 3 different variables?