问题描述
说:
That means, an extern
declaration that initializes the variable serves as a definition for that variable. So,
/* Just for testing purpose only */
#include <stdio.h>
extern int y = 0;
int main(){
printf("%d\n", y);
return 0;
}
should be valid (compiled in C++11). But when compiled with options -Wall -Wextra -pedantic -std=c99
in GCC 4.7.2, produces a warning:
[Warning] 'y' initialized and declared 'extern' [enabled by default]
which should not. AFAIK,
extern int y = 0;
is effectively the same as
int i = 0;
What's going wrong here ?
All three versions of the standard — ISO/IEC 9899:1990, ISO/IEC 9899:1999 and ISO/IEC 9899:2011 — contain an example in the section with the title External object definitions (§6.7.2 of C90, and §6.9.2 of C99 and C11) which shows:
The example continues, but the extern int i3 = 3;
line clearly shows that the standard indicates that it should be allowed. Note, however, that examples in the standard are technically not 'normative' (see the foreword in the standard); they are not a definitive statement of what is and is not allowed.
That said, most people most of the time do not use extern
and an initializer.
这篇关于如何与声明一起定义外部变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!