2

a global variable may one to two different storage classes in C, to my best knowledge, and the declaration may be given with two different keywords, correspodingly

extern int foo; //default
static int bar;

Static variables are only visible within the module of declaration, and cannot be exported. In case of extern declaration, the variable is in the common namespace of all modules linked, unless shadowed by static variable.

Whereas static variables have to be defined in their module, an extern variable may be defined somewhere else. It has to be defined if ever used.

My compiler (GCC) accepts

static int bar = 5;

but casts a complain at

extern int foo = 4;

It seems to be expected that extern variables are never defined with the keyword 'extern'. This leads to the following question:

What kind of storage class does the Object 'foo' in the example above have in the module where it is defined?

shuhalo
  • 5,732
  • 12
  • 43
  • 60
  • 3
    What is the complaint? I suspect it complains about it being defined multiple times. – Skurmedel Apr 12 '11 at 11:38
  • 4
    "extern" is effectively "someone else will have defined this" - so it doesn't make sense that you try to assign a value in an extern declaration. You simply do "int foo = 4;" in *one* C file, and you're fine. – Erik Apr 12 '11 at 11:40
  • 1
    You can't set the the value of an extern, you don't own it's instantiation the module that declares it normally does – forsvarir Apr 12 '11 at 11:40
  • You miss the question: When I write this line outside any function: int foo = 4; what is the storage class of foo? It is not static, but it can't be extern. – shuhalo Apr 19 '11 at 10:47

4 Answers4

2

IIRC, extern is more of a hint to the compiler that it does not have to allocate storage for the value. The linker is expected to find the value in another compilation unit. Usually extern is used in header files to indicate that the someone has defined storage associated with the name. The definition of the value does not include the extern keyword since the compiler has to allocate storage for the value in the compilation unit that includes the definition.

See extern storage class specifier for more details.

Community
  • 1
  • 1
D.Shawley
  • 58,213
  • 10
  • 98
  • 113
0

The extern variable will be defined with a global scope (exported) in the unit where it is defined:

int baz = 5;

gby
  • 14,900
  • 40
  • 57
  • Now, is baz a static variable or an extern variable? If neither, what is it then? – shuhalo Apr 19 '11 at 10:49
  • in the file it is defined it is a static variable. In other complication units, it's an extern. Not to be confused with the use of the "static" keyword which, while defines the variable to be static, also limits its scope to the local compilation unit. " – gby Apr 19 '11 at 19:29
0

The default storage class is auto.

Oliver Charlesworth
  • 267,707
  • 33
  • 569
  • 680
0

Actually you missed two storage classes: auto and register
Register doesn't matter here but the default storage class is auto.
Auto reserves space for a variable somewhere in the memory (which is usually what you want when you declare a variable). It should be noted that for 'auto variables' new space will be allocated every time the scope of the variable is entered. (i.e. calling the function func() from within func() when func() declares an 'auto' variable will result in two different variables and each call to func() will only know about its own variable.
From this follows that auto variables declared at the global scope will be unique (since the scope is enterd only once).
Static variables however are always unique. Unique in the sense that space will only be allocated once. This is useful when func() calls func() and you want both function calls to operate on the same variable.
Extern variables are simply references to unique variables.
You use these when you want to access a global variable declared in a different file.
Given the files 1.c and 2.c it does not suffice to declare "int global;" in both files because space would be allocated twice and the name clash would result in a linking error.
Hence what you do is in one file to reserve space (using "int global;") and in the other file tell the linker to look for a variable of the name "global" in another file by writing "extern int global;".

iolo
  • 1,090
  • 1
  • 9
  • 20