How to define extern variable along with declaration?

后端 未结 2 858
北荒
北荒 2021-01-04 02:35

Wiki says:

The extern keyword means \"declare without defining\". In other words, it is a way to explicitly declare a variable, or to f

2条回答
  •  佛祖请我去吃肉
    2021-01-04 02:59

    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:

    EXAMPLE 1

    int i1 = 1;        // definition, external linkage
    static int i2 = 2; // definition, internal linkage
    extern int i3 = 3; // definition, external linkage
    int i4;            // tentative definition, external linkage
    static int i5;     // tentative definition, internal linkage
    

    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.

提交回复
热议问题