0 day streak
c / intermediate
Snippet

Internal Linkage with the static Keyword

Using 'static' at the file level (global scope) gives a variable or function internal linkage. This means it is only visible within that specific .c file, preventing name collisions and enforcing encapsulation in modular programming.

snippet.c
c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <stdio.h>
// Internal linkage: hidden from other translation units
static int module_state = 0;
static void log_internal(void) {
printf("State updated to: %d\n", module_state);
}
void public_increment(void) {
module_state++;
log_internal();
}
int main(void) {
public_increment();
return 0;
}
Breakdown
1
static int module_state
Variable is stored in the data segment but is not exported to the linker.
2
static void log_internal
Function cannot be called by code in other source files.