Let us say I have only one file in my project called test.c; the code below does not compile if I do not define "TRUE". I just want to understand the behavior. Please throw some light on this aspect.
#ifdef TRUE
static int a;
extern int a;
#else
extern int a;
static int a;
#endif
int main (void)
{
a =10;
printf("%d", a);
return 0;
}
When TRUE is not defined, the first declaration (extern) says a has external linkage (ISO/IEC 9899:1999, 6.2.2, paragraph 4, no prior declaration). The second declaration (static) states a has internal linkage (paragraph 3). An identifier cannot have both internal and external linkage (paragraph 7).
In the TRUE defined case, the extern in the second declaration has no impact because there is a prior declaration declaring a with internal linkage (paragraph 4).
See draft of ISO/IEC 9899:1999.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With