C Static Variables

A static variable has static storage duration, so it exists for the entire execution of the program.

A local variable declared with static keeps its value between function calls while its name remains local to that block. Compare this with Local Variables and Global Variables.

What Is a Static Variable in C?

A static variable is an object with static storage duration. It exists for the whole time the program is running.

For beginners, the most useful example is a local static variable. It is declared inside a function, but its stored value does not disappear when the function returns.

When the function is called again, the same static variable still has the value left by the previous call.

C Static Variable Syntax

A local static variable is declared with the static keyword:

static data_type variable_name = value;

For example, static int count = 0; creates a local static integer variable named count.

How a local static variable keeps its value between C function calls A timeline diagram showing one static count variable starting at zero, then three calls to showCount. The same variable changes to one, two, and three while remaining alive between calls. STATIC VARIABLE ACROSS CALLS One variable survives while function calls come and go PERSISTENT LOCAL STORAGE static int count = 0; Block scope • static storage duration Call 1 count: 0 → 1 Function returns Call 2 count: 1 → 2 Same variable returns Call 3 count: 2 → 3 Value is retained again KEY DIFFERENCE Ordinary local: new lifetime each call • Local static: same stored object for the whole program

C Static Variable Example

This program calls the same function three times. The local static variable count keeps its value between calls.

#include <stdio.h> 

void showCount(void) { 
    static int count = 0; 

    count++; 

    printf("%d\n", count); 
} 

int main(void) { 
    showCount(); 
    showCount(); 
    showCount(); 

    return 0; 
}

Output

1 
2 
3

The first call changes count from 0 to 1. The second call starts with the retained value 1, and the third call starts with 2. The variable is not reset to 0 on each call.

Scope of a Local Static Variable

A local static variable still has local block scope.

This means the name count in the example can be used only inside showCount(). Code in main() cannot use that local name directly.

Lifetime of a Static Variable

A static variable has static storage duration, so its lifetime lasts for the entire execution of the program.

This is why a local static variable can remember its stored value after the function returns and use that value again on a later call.

Static Local Variable vs Ordinary Local Variable

An ordinary local variable normally gets a new lifetime each time execution enters its block. A local static variable keeps the same stored object for the entire program.

Both can have local block scope, but their storage duration is different.

Default Value of a Static Variable

If a static variable has no explicit initializer, C initializes it to zero before program execution begins.

For example, static int count; starts with the value 0.

A Short Note About static Outside Functions

The static keyword can also be used at file scope. There, it affects linkage and keeps the name limited to the current source file.

That is a different use of the same keyword. On this page, the main focus is the beginner-friendly local static variable that keeps its value between function calls.

Common Static Variable Mistakes

Common mistakes when using static variables in C
Mistake Correct Idea
Expecting a local static variable to reset on every function call. It keeps its stored value between calls.
Thinking local static makes the variable visible everywhere. Its name still has local block scope.
Confusing the lifetime of a static variable with the lifetime of an ordinary local variable. A static variable exists for the entire program execution.

Short Practice

  1. Inside a function, declare a local static int variable named visits and initialize it to 0.
  2. If a static variable changes from 2 to 3 during one call, what value will it still have at the start of the next call?
  3. Can main() directly use the name of a local static variable declared inside another function?

FAQs

What is a static variable in C?

A static variable has static storage duration, so it exists for the entire execution of the program. A local static variable keeps its value between calls to the function.

Does a local static variable keep its value between function calls?

Yes. A local static variable is not recreated with a fresh value on every call. Its stored value remains available for later calls.

What is the scope of a local static variable in C?

A local static variable still has block scope, so its name can be used only inside the block where it is declared.

What value does an uninitialized static variable get in C?

A static variable is automatically initialized to zero if no explicit initializer is provided.

Summary

A C static variable has static storage duration and exists for the entire execution of the program. A local static variable keeps its value between function calls while its name remains limited to its local block. If no initializer is written, the static variable is automatically initialized to zero.