Please refer to the following code that is in the same translation unit:
static int global_var; // file scope in C and global namespace scope in C++
Generally static
variables have internal linkage. You can't access the static
variable or function in another file(in multiple file compilation situations), because its scope limited within this file(Internal linkage).
Normally auto
and register
variables have no linkage.
As i said above auto
and register
variables have no linkage. You can't declare these variables in global scope. static
variables has internal linkage, scope is based on declaration, but not possible to access in another file. extern
variables has external linkage, its possible to access these variables in another file.
For more reference Storage classes
global_var
could be accessed from void g()
in the same compilation unit, that is the difference.
First: in addition to type, variables have three other characteristics: linkage, scope and lifetime. All four attributes are sort of orthogonal, but linked in the way they are expressed in the language, and do interact in some ways.
With regards to linkage: linkage really affects the symbol which is being declared, and not the object itself. If there is no linkage, all declarations of the symbol bind to different objects, e.g.:
int
func()
{
int i;
{
int i;
}
}
The symbol i
has no linkage, and the two symbols i
are bound
to two different entities. Generally speaking, local variables
(variables declared at block scope) and function arguments have
no linkage, regardless of type and lifetime.
Internal and external linkage are similar, in that repeated declarations of the symbol bind to the same entity: internal linkage binds only within the translation unit, external accross the entire program. So given:
static int i; // internal linkage...
in several translation units, the i
binds to a separate entity
in each translation unit. Without the static, you have external
linkage, and all of the i
bind to the same entity.
Note that this only holds at namespace scope; all entities which are members of a non-local class have external linkage.
And that type has an impact: variables which are const implicitly have internal linkage:
int const i = 42; // same as static int const i...
extern int const j = 42; // external linkage.
Finally, all declarations which bind to the same entity must declare it to have the same type. If you violate this rule in a single translation unit (e.g.:
extern int i;
// ...
double i;
in the same namespace scope), then the compiler should complain. If the two declarations are in different translation units, however, it is undefined behavior, and who knows what will happen. (In theory, the linker could complain, but most don't.)
EDIT:
One additional point: linkage is determined by the first declaration which can refer to the entity. So if I write:
static int i;
void
func()
{
extern int i;
}
Both i
refer to the same entity, which has internal linkage.
(Why one would ever write the second declaration is beyond me,
but it is legal.)