Are global variables extern by default or is it equivalent to declaring variable with extern in global?

前端 未结 2 1674
抹茶落季
抹茶落季 2020-12-09 19:36

I have gone through following two questions:

static and extern global variables in C and C++

global variable in C are static or not?

Both questions s

2条回答
  •  执笔经年
    2020-12-09 20:17

    is global variables are extern by default in linkage (or) it is equivalent to declaring variable by specifying extern storage class?

    Unless otherwise specified, they have external linkage (except in C++, where they have internal linkage if they're constant).

    The first answer you link to is saying that it's not equivalent to declaring it extern (which makes it a pure declaration, not a definition); not that it doesn't have external linkage.

    is global variables are static by default in scope (or) it is equivalent to declaring variable by specifying static storage class?

    In C++, they have internal linkage (as if declared static) if they are constant, external linkage otherwise. In C, they always have external linkage.

    If there is any c or c++ difference please clarify?

    As mentioned above, the default is always external linkage in C, while in C++ it's internal for constant variables.

提交回复
热议问题