Use of 'extern' keyword while defining the variable

前端 未结 2 501
既然无缘
既然无缘 2020-12-12 01:11

After seeing this answer I have this doubt. In my project, I have seen some extern variables declared and defined like below:

file1.h

2条回答
  •  春和景丽
    2020-12-12 01:37

    It does not change the meaning. extern only makes sense when you declare a variable. Defining a variable with extern is the same because all global variables that are not marked static are symbols visible to the linker by default.

    Note that if you didn't want to initialise the variable, that is, not having the part = 10, the compiler will assume that extern int a is always a declaration and not a definition. In the same sense, having int a globally is always a definition and not just a declaration.

提交回复
热议问题