On this page
static
Prev: const | Up: builtins | Next: extern
Rel: Control Flow & Functions, extern
Ref: cppreference — static
Tags: #public #prog #c #builtins #tier/core
Availability
Built-in (Storage-Class Specifier) — requires no #include header.
Syntax & Overview
static T x; | static T fn(void);
Gives file-scope functions/variables internal linkage (private to the .c file), or gives block-scope locals static storage duration.
Notes & Pitfalls
- Two meanings: At file scope, restricts symbol visibility to the current
.cfile (internal linkage); inside a function, preserves the variable's value across calls (not thread-safe without synchronization).
Example
#include <assert.h>
static int next_id(void) {
static int id = 0;
return ++id;
}
int main(void) { assert(next_id() == 1 && next_id() == 2); }
Linked from
Related notes
- tuts/c/builtins/const Linked here; links to this page; shared tags: #builtins, #c, #prog, #tier
- tuts/c/builtins/extern Linked here; links to this page; shared tags: #builtins, #c, #prog, #tier
- tuts/c/builtins Linked here; links to this page; shared tags: #builtins, #c, #prog
- tuts/c/fundamentals/control_flow_and_functions Linked here; shared tags: #c, #prog
- tuts/c/builtins/__FILE__ shared tags: #builtins, #c, #prog, #tier
- tuts/c/builtins/__func__ shared tags: #builtins, #c, #prog, #tier
- tuts/c/builtins/__LINE__ shared tags: #builtins, #c, #prog, #tier
- tuts/c/builtins/__STDC_VERSION__ shared tags: #builtins, #c, #prog, #tier
- tuts/c/builtins/_Alignof shared tags: #builtins, #c, #prog, #tier
- tuts/c/builtins/_Atomic shared tags: #builtins, #c, #prog, #tier