The static storage class in C helps keep a variable or function's value or state between uses, and controls its visibility in the program.
Static storage class
static data_type variable_name = initial_value;The static keyword can be used with variables inside functions or outside functions.
Static variables inside functions keep their value between calls but are only visible inside that function.
void count_calls() { static int count = 0; count++; printf("Called %d times\n", count); }
static int file_var = 10; void print_var() { printf("Value: %d\n", file_var); }
This program shows how a static variable inside a function keeps its value between calls.
#include <stdio.h> void demo_static() { static int counter = 0; counter++; printf("Counter is %d\n", counter); } int main() { demo_static(); demo_static(); demo_static(); return 0; }
Static variables are initialized only once, when the program starts or when the function is first called.
Static variables inside functions are not destroyed when the function ends.
Using static for global variables or functions limits their scope to the file, helping avoid name conflicts.
Static keeps variables alive between function calls.
Static limits variable or function visibility to the file or function.
Use static to remember values or hide details inside a file.