An object having static storage duration means that its life time would be throughout the entire program. It gets created when the program starts and destroyed when the program ends.

Almost all types of global variables have static duration. Local variables can be made static using static keyword as shown in the example.

Given the property it has of initializing value for just once, it can be used to record how many times a function is being called for profiling.

For example,

#include "../helpers/stdout.h"
 
int callingMeCounts(void)
{
    static int s_calls{0};
    return ++s_calls;
}
 
int main(int argc, char const *argv[])
{
    print(callingMeCounts());
    print(callingMeCounts());
    print(callingMeCounts());
    print(callingMeCounts());
    return 0;
}
 

Note

Static local variables are prefix with s_, similar to global variables g_.

References

  1. https://en.cppreference.com/w/c/language/static_storage_duration.html