Quick Contact

✉
fahimkhan20148@gmail.com
📱
+971 507 286 133
Back to Notes
August 23, 2026
cmemorystorage-classes

Static Storage Class in C

A static lifetime means the value will exist until the end of the program, so any value with a static lifetime can be shared across threads.

Any variable declared static exists before main and remains in scope until the program ends. Values can also be leaked on the heap to guarantee they exist for the program's entire duration.

static has different behaviour for local and global variables.

void foo(void) {
	static int i = 0;
	printf("i = %d\n", i);
	i++;
}

int main(void) {
	foo(); //prints i = 0
	foo(); //prints i = 1
	foo(); //prints i = 2
	foo(); //prints i = 3
	
	return 0;
}

Local variables with the static modifier retain their value between function executions, unlike auto variables that are allocated on the stack; local static variables are defined in the data segment (or BSS segment). They stay from the start to the end of the program.

The whole struct can have the static modifier but individual members can't have a static modifier due to the alignment rule.

static variables have to be known at compile time, so

static int i = get_i();

will not work.

// main.c
static int i = 10;
static int foo(void) {
	return 10;
}

Both the variable i and the function foo are now visible only in main.c, the file where they are defined.