warning in extern declaration

后端 未结 4 808
情歌与酒
情歌与酒 2020-11-29 12:12
#include
#include
#define GREY 1
#define BLACK 0
#define WHITE 2
typedef struct node * graph;
typedef struct stack * snode;

graph cno         


        
4条回答
  •  猫巷女王i
    2020-11-29 12:49

    Clang still issues a warning for this. The line

    extern int counter = 0;
    

    will trigger the warning:

    warning: 'extern' variable has an initializer [-Wextern-initializer]

    This warning is not important, as defining the variable with

    int counter = 0;
    

    still yields a static duration and external linkage by default. Indeed, if no storage-class specifier is provided, the defaults are:

    • extern for all functions
    • extern for objects at file scope
    • auto for objects at block scope

    There is also something called a tentative definition which is an external declaration without an initializer, and either without a storage-class specifier or with the specifier static.

    A tentative definition is a declaration that may or may not act as a definition. If an actual external definition is found earlier or later in the same translation unit, then the tentative definition just acts as a declaration.

    So the following line

    int counter;
    

    is a tentative definition that declares and defines counter with the implicit initializer = 0 (or, for array, structure, and union types, = {0}).

提交回复
热议问题