Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

static extern vs extern static

Tags:

c

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;
}
like image 493
ECC Avatar asked Feb 18 '26 01:02

ECC


1 Answers

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.

like image 80
undur_gongor Avatar answered Feb 19 '26 20:02

undur_gongor



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!