CKTIS / C
extern — declarations and linkage
extern declares objects or functions; it does not automatically mean a definition or a location in another file.
Version: C23; repository compiler flag, production runtime not attested
Syntax
extern int counter;
extern int read_counter(void);extern is a storage-class specifier. extern int counter; declares an object without defining it. Its definition may occur in the same or another translation unit.
An extern declaration inherits the linkage of a visible prior declaration of the same identifier only if that prior declaration has internal linkage (for example, from static) or external linkage. If no prior declaration is visible, or if the visible prior declaration has no linkage, the extern declaration has external linkage. A function declaration without a storage-class specifier determines linkage as though declared extern.
At file scope, extern int counter = 7; is a definition, whereas int counter; is a tentative definition. Typically, a header contains the extern declaration and one source file contains the definition. The object used here requires exactly one definition in the program.
Examples
#include <stdio.h>
extern int counter;
extern int read_counter(void);
int main(void)
{
printf("%d\n", read_counter());
return 0;
}
int counter = 7;
int read_counter(void)
{
return counter;
}
Result
7
The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.