Static Variables
In C, a static variable is a special type of variable that retains its value across function calls. Unlike local variables, which are recreated every time a function is called, static variables are initialized only once and maintain their value for the lifetime of the program. Static variables can be declared inside functions, blocks, or globally, but their behavior differs based on their scope.
Static variables are often used to store data that needs to persist, like counters or state information.
Syntax
static data_type variable_name = value;
static
: A keyword that declares the variable as static, ensuring it retains its value across function calls and persists for the program’s lifetime.data_type
: Specifies the type of the variable (e.g.,int
,float
,char
).variable_name
: The name of the variable, used to access it.value
(Optional): The initial value assigned to the variable. If not provided, static variables are automatically initialized to zero.
Example
Here’s an example demonstrating the use of static variables:
#include <stdio.h>void counter() {static int count = 0; // Static variable retains its valuecount++;printf("Count: %d\n", count);}int main() {counter(); // Output: Count: 1counter(); // Output: Count: 2counter(); // Output: Count: 3return 0;}
The output of the above code will be:
Count: 1Count: 2Count: 3
The code demonstrates the use of a static variable count
inside the counter
function, which retains its value across multiple function calls. Each time counter()
is called, count
increments and prints its updated value, showcasing the persistence of static variables.
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.