8

static is the default storage class for global variables. The two variables below (Count and Road) both have static storage class.

static int Count;
int Road;
int main()
{
    printf("%d\n", Road);
    return 0;
}

My question is: if by default global variables are static (which means we are limiting the scope of that global variable to that particular .c file) then how can we extern those variables in another file?

This question might be very basic to many of you but I am really confused and want to learn the correct details.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
user968000
  • 1,765
  • 3
  • 22
  • 31
  • read( http://stackoverflow.com/questions/2271902/static-vs-global && http://stackoverflow.com/questions/4239834/global-variable-in-c-are-static-or-not ) :) – VoidPointer Jun 19 '13 at 04:03
  • 1
    Your first sentence is false. A variable declared `static` at file scope is *not* a global variable. – William Pursell Jun 19 '13 at 04:08
  • 2
    Your source is wrong. Variables and functions declared at the file scope have `extern` storage class by default (although that gives them static *duration*). The `static` keyword gives them static storage class. – hobbs Jun 19 '13 at 04:16
  • @hobbs: An object declared at file scope as external *linkage* and static *storage duration*. – Keith Thompson Sep 15 '13 at 01:19

2 Answers2

17

In the formal C terminology specifiers like extern, static, register etc. are called storage-class specifiers, but the actual object properties these specifiers control are called storage duration and linkage.

In your question you seem to be mixing these two unrelated concepts: storage duration and linkage. It is actually linkage that describes external visibility of the object.

All variables defined in file scope have static storage duration (regardless of whether you used the keyword static in the declaration). This simply means that they live forever, but it does not say anything about their external visibility. Meanwhile, variables defined with keyword static have internal linkage, while variables defined without any keywords or with keyword extern have external linkage.

In your example variable Road has static storage duration and external linkage, which is why you can access it directly from other translation units. Variable Count has static storage duration and internal linkage, which is why you can't access it directly from other translation units.

If you declare a variable without a storage class specifier (like Road in your example), it will be treated as so called tentative definition and finally resolve (in your example) to a variable with static storage duration and external linkage. So, from that point of view it is right to say that the default (implied) storage class specifier for file scope variables is actually extern, not static.

AnT stands with Russia
  • 312,472
  • 42
  • 525
  • 765
  • If C "does not have such term as 'storage class'" this answer cannot be correct. – hobbs Jun 19 '13 at 06:03
  • @hobbs: Sorry for my confusion. Your comment under the original question is correct, in a sense that variables declared without storage class specifier behave as if they vere declared with `extern` storage class specifier (of course, things get a bit more complicated when we take into account tentative definitions, but that's a different story). – AnT stands with Russia Jun 19 '13 at 06:51
7

The variable Count is only accessible by name within this one source file because of the static that precedes it. Formally, it is said to have internal linkage (see ISO/IEC 9899:2011 §6.2.2 Linkages of Identifiers).

The variable Road could be accessed from other source files if those files included the equivalent of extern int Road; as one of the statements. Formally, it is said to have external linkage.

Generally, most people would call Count a static variable and Road a global variable.

See also What are extern variables in C?

Community
  • 1
  • 1
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • so what is the default storage class for global variables then? – user968000 Jun 19 '13 at 04:09
  • 1
    'Global variable' variable is not a term defined in Standard C. However, variables with external linkage are what most people think of as global variables, so their storage class is `extern`. – Jonathan Leffler Jun 19 '13 at 04:11