18

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 says the two things in different way.

Question 1's Answer:

Global variables are not extern nor static by default on C and C++.

Question 2's Answer:

If you do not specify a storage class (that is, the extern or static keywords), then by default global variables have external linkage

I need to know the following:

  1. Are global variables extern by default in linkage (or) is it equivalent to declaring variables by specifying extern storage class?
  2. Are global variables static by default in scope (or) is it equivalent to declaring variables by specifying static storage class?
  3. Is there any difference with C or C++? Can anyone please clarify?
Keale
  • 3,924
  • 3
  • 29
  • 46
VINOTH ENERGETIC
  • 1,775
  • 4
  • 23
  • 38

3 Answers3

13

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

The default storage duration, scope and linkage of variables declared outside any block, at the outer most level, have static storage duration, file scope and external linkage. C11 standard says that:

6.2.1 Scopes of identifiers (p4):

[...] If the declarator or type specifier that declares the identifier appears outside of any block or list of parameters, the identifier has file scope, which terminates at the end of the translation unit. [...]

6.2.2 Linkages of identifiers (p5):

[...] If the declaration of an identifier for an object has file scope and no storage-class specifier, its linkage is external.

6.2.4 Storage durations of objects (p3):

An object whose identifier is declared without the storage-class specifier _Thread_local, and either with external or internal linkage or with the storage-class specifier static, has static storage duration.

So, if x is global

int x;

then its storage duration, scope and linkage is equivalent to x in

extern int x;   

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

No. As I stated above that its duration is static and it has file scope.

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

No difference. Rule is same in both languages.

Community
  • 1
  • 1
haccks
  • 104,019
  • 25
  • 176
  • 264
  • 2
    From the way this reads, your third section (6.2.4) reads as though the statement `int x;` is equivalent to `extern int x;`. This will result in linkage error(s) as `int x;` generates the symbol with the correct space while `extern int x;` only produces a reference to a symbol defined elsewhere. – Mark Feb 19 '15 at 16:26
  • 3
    You're missing a third important distinction: declaration vs. definition. `extern int i;` is not a definition, so `i` must be defined elsewhere. `int i;` or `extern int i = 42;` are definitions, and `i` may not be defined elsewhere. – James Kanze Feb 19 '15 at 17:21
  • 1
    @Mark; There was some error in previous answer. Corrected that. See the revision history. – haccks Feb 19 '15 at 17:53
7

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.

Mike Seymour
  • 249,747
  • 28
  • 448
  • 644
0

Verified by using gcc version9.3.0:

Global variables are extern by default.

Youjun Hu
  • 991
  • 6
  • 18