In the following program, I thought that extern int i;
will change the following i
to refer to the i
defined outside main
:
#include <stdio.h>
extern int i=1; // warning: 'i' initialized and declared 'extern'
int main()
{
int i=2;
printf("%d
", i);
extern int i; // error: extern declaration of 'i' follows declaration with no linkage
printf("%d
", i);
return 0;
}
What is the reason of the "error: extern declaration of 'i' follows declaration with no linkage", where "declaration with no linkage" refers to int i=2;
?
After I remove int i=2
in main
,
- the error is gone,
- the warning "warning: 'i' initialized and declared 'extern'" on
extern int i=1;
also disappear . Why is that?
Thank you for explanations!
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…