When we declare any global variable, for instance
int x;
it is equivalent to
extern int x;
Now by default global variables are initialized to 0 by the compiler, which means they are allocated memory. But if I simply write
extern int x;
then this will only declare the variable, while no memory would be allocated to it. So, my query is that if I write extern before int x
or I do not write it, in case of global variables, how is the compiler treating them differently? In the case where I simply write int x
, it allocates memory and simultaneously it puts extern before int x
, while in the case where I write extern int x
, it only declares the variable while no memory is allocated to it. Please clarify how the compiler is behaving in both ways.