Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Concept Entry] C: Static Variables #5982

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions content/c/concepts/static-variables/static-variables.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# Static Variables in C

## Introduction
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
Here’s the basic syntax for declaring a static variable:

```c
static data_type variable_name = value;
```

* `static`: The keyword that makes the variable static.
* `data_type`: The type of the variable (e.g., `int`, `char`, etc.).
* `variable_name`: The name of the variable.
* `value`: (Optional) The initial value of the variable. If not explicitly initialized, static variables are automatically initialized to zero.

## Example
Here’s an example demonstrating the use of static variables:

```c
#include <stdio.h>

void counter() {
static int count = 0; // Static variable retains its value
count++;
printf("Count: %d\n", count);
}

int main() {
counter(); // Output: Count: 1
counter(); // Output: Count: 2
counter(); // Output: Count: 3
return 0;
}
```

### Explanation:

* The variable `count` is declared as `static`. This means it is initialized only once (to 0) and keeps its value between function calls.
* Every time `counter()` is called, `count` increments and retains its updated value.