Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C

declarations and linkage — concept C23

A declaration binds an identifier to a type, scope, and linkage; a definition additionally provides the object or function body. `extern` can declare a definition in another translation unit, file-scope `static` gives internal linkage, and complex declarators are read outward from the name.

Version: C23

Syntax

declarations and linkage

A declaration binds an identifier to a type, scope, and linkage; a definition additionally provides the object or function body. extern can declare a definition in another translation unit, file-scope static gives internal linkage, and complex declarators are read outward from the name.

Put a prototype such as int read_value(void); in the header and its definition in exactly one source file; other files obtain the compatible type by including that header. For shared data, accessor functions are usually safer than a writable extern variable.

An incomplete array declaration, a tentative definition, and a full definition have different effects, so do not duplicate them casually. int *a, b; makes only a a pointer; use a typedef or split declarations for function and array pointers to prevent misreading.

Examples

#include <stdio.h>

static int shared_value = 7;
extern int read_value(void);
int read_value(void) { return shared_value; }

int main(void) { printf("%d\n", read_value()); return 0; }

Result

7

A declaration binds an identifier to a type, scope, and linkage; a definition additionally provides the object or function body. `extern` can declare a definition in another translation unit, file-scope `static` gives internal linkage, and complex declarators are read outward from the name.

Sources