Namespaces
Variants
Actions

Static storage duration

From cppreference.com
< c‎ | language
 
C
 
C language

An object whose identifier is declared without the storage-class specifier _Thread_local, and either with external or internal linkage or with the storage-class specifier static, has static storage duration. Its lifetime is the entire execution of the program and its stored value is initialized only once, prior to program startup.

[edit] Notes

Since its stored value is initialized only once, an object with static storage duration can profile the invocations of a function.

The other use of the keyword static is file scope.

[edit] Example

#include <stdio.h>
 
void f (void)
{
    static int count = 0;   / static variable   
    int i = 0;              / automatic variable
    printf("%d %d\n", i++, count++);
}
 
int main(void)
{
    for (int ndx=0; ndx<10; ++ndx)
        f();
}

Output:

0 0
0 1
0 2
0 3
0 4
0 5
0 6
0 7
0 8
0 9

Follow Lee on X/Twitter - Father, Husband, Serial builder creating AI, crypto, games & web tools. We are friends :) AI Will Come To Life!

Check out: eBank.nz (Art Generator) | Netwrck.com (AI Tools) | Text-Generator.io (AI API) | BitBank.nz (Crypto AI) | ReadingTime (Kids Reading) | RewordGame | BigMultiplayerChess | WebFiddle | How.nz | Helix AI Assistant