21

Are global variables in C static or extern by default?
If global variables are by default static then it means that we would be able to access them in a single file, but we can use global variables in different files as well.
Does this imply that they have extern storage by default?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Mishthi
  • 1,947
  • 5
  • 16
  • 12

3 Answers3

38

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

§6.2.2 Linkages of identifiers

3) If the declaration of a file scope identifier for an object or a function contains the storage-class specifier static, the identifier has internal linkage.

5) If the declaration of an identifier for a function has no storage-class specifier, its linkage is determined exactly as if it were declared with the storage-class specifier extern. If the declaration of an identifier for an object has file scope and no storage-class specifier, its linkage is external.

So even if you don't specify the extern keyword, globals can still be accessed by other source files (so-called translation units), because they can still have an extern declaration for the same variable. If you use the static keyword to specify internal linkage, then even in the presence of an extern declaration for the same variable name in another source file, it will refer to a different variable.

Adam Rosenfield
  • 390,455
  • 97
  • 512
  • 589
  • 1
    also relevant: section 6.9.2, which describes tentative definitions; §4 of that section shows explicitly how multiple definitions of file-scope variables interact – Christoph Nov 21 '10 at 19:51
  • @Mishthi: Do you mean a variable that is both static and extern? I'm afraid that's not possible. – Saurabh Manchanda Nov 21 '10 at 20:01
  • 1
    @Mishthi: `static extern varx` is undefined behavior. A variable cannot be both `static` and `extern`, it has to be one or the other. – Adam Rosenfield Nov 21 '10 at 20:02
  • Is this also true in C++? Or just C? – SSH This Apr 13 '12 at 21:01
  • @SSHThis: Yes, the same is true in C++. See C++03 §3.5 [basic.link]. – Adam Rosenfield Apr 15 '12 at 01:19
  • 1
    Actually, `static extern varx;` is not undefined behaviour; it is a constraint violation. C11 [§6.7.1 Storage classes ¶2](http://port70.net/~nsz/c/c11/n1570.html#6.7.1p2): _At most, one storage-class specifier may be given in the declaration specifiers in a declaration, except that `_Thread_local` may appear with `static` or `extern`._ . There's also a problem with 'implicit int' type in the attempted declaration — that's not been legal in standard C in the current millennium (it was outlawed by C99, though some compilers still allow it unless prodded into rejecting it). – Jonathan Leffler May 17 '19 at 18:51
7

In C, a global variable which doesn't have an initializer or any storage class specifiers is a tentative definition of a variable with static storage duration and external linkage.

In a translation unit all tentative definitions and up to one non-tentative definition (e.g. from a declaration with an initializer) are collapsed into a single definition for a variable. Although it's not allowed to have a definition of the same variable in multiple translation units it is a common extension to allow "common" variables, i.e. tentative definitions of the same variable in multiple translation units.

CB Bailey
  • 755,051
  • 104
  • 632
  • 656
4

Global variables in C are by default extern.. (i.e) they have external linkage..

To restrict the external linkage, 'static' storage class specifier can be used for the global variable.. if static specifier is used, then the variable has file scope.. You cannot link it in an other file using the 'extern' keyword..

Specifying 'static' depends on your usage of the program..

Raghu Srikanth Reddy
  • 2,703
  • 33
  • 29
  • 42