In C programming, an identifier declared in different scopes or in the same scope more than once can refer to the same object or function through a process called linkage. There are three kinds of linkages: external, internal, and none.
Types of Linkages
- External Linkage: In the set of translation units and libraries that constitutes an entire program, each declaration of a particular identifier with external linkage denotes the same object or function. If an identifier is declared with the storage-class specifier `extern` in a scope where a prior declaration of that identifier is visible, and if the prior declaration specifies internal or external linkage, the linkage of the identifier at the later declaration is the same as the linkage specified at the prior declaration. If no prior declaration is visible, or if the prior declaration specifies no linkage, then the identifier has external linkage.
- Internal Linkage: Within one translation unit, each declaration of an identifier with internal linkage denotes the same object or function. 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.
- No Linkage: Each declaration of an identifier with no linkage denotes a unique entity. The following identifiers have no linkage: an identifier declared to be anything other than an object or a function; an identifier declared to be a function parameter; a block scope identifier for an object declared without the storage-class specifier `extern`.
Special Cases
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.
Undefined Behavior
If, within a translation unit, the same identifier appears with both internal and external linkage, the behavior is undefined. This is a scenario that programmers should avoid to ensure the predictability and correctness of their programs.