In certain circumstances, the C language allows you to declare an object such as a variable without defining it.
This can be done using the keyword ‘extern’ or through the tentative definition rule for global variables (although in this case I am not sure if it's allowed by the language or the compiler. It works for me with GCC -std=iso9899:2017 -pedantic-errors).
So we can compile this without error:
int main(void)
{
extern int a;
extern int a;
extern int a;
extern int a;
extern int a;
extern int a;
}
Or this:
int a;
int a;
int a;
int a;
int main(void)
{
a = 0;
}
Unless I am mistaken, in that case the compiler treats each 'int a;' as an attempt of definition until it reaches the last one which is treats as an actual definition. This means that previous attempts are regarded as mere declarations.
But if we try this with a non global object, it triggers an error of redeclaration with no linkage.
So, is redeclaration of global objects legal or illegal according to C standards? And if it is, why only for global objects?
Thank you in advance for your help.