Difference between internal and no linkage Difference between internal and no linkage c c

Difference between internal and no linkage


First: in addition to type, variables have three othercharacteristics: linkage, scope and lifetime. All fourattributes are sort of orthogonal, but linked in the way theyare expressed in the language, and do interact in some ways.

With regards to linkage: linkage really affects the symbol whichis being declared, and not the object itself. If there is nolinkage, all declarations of the symbol bind to differentobjects, e.g.:

intfunc(){    int i;    {        int i;    }}

The symbol i has no linkage, and the two symbols i are boundto two different entities. Generally speaking, local variables(variables declared at block scope) and function arguments haveno linkage, regardless of type and lifetime.

Internal and external linkage are similar, in that repeateddeclarations of the symbol bind to the same entity: internallinkage binds only within the translation unit, external accrossthe entire program. So given:

static int i;   //  internal linkage...

in several translation units, the i binds to a separate entityin each translation unit. Without the static, you have externallinkage, and all of the i bind to the same entity.

Note that this only holds at namespace scope; all entitieswhich are members of a non-local class have external linkage.

And that type has an impact: variables which are constimplicitly 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 mustdeclare it to have the same type. If you violate this rule ina 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 willhappen. (In theory, the linker could complain, but most don't.)

EDIT:

One additional point: linkage is determined by the firstdeclaration which can refer to the entity. So if I write:

static int i;voidfunc(){    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.)


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.