There are two issues here, lifetime and scope.

The scope of variable is where the variable name can be seen. Here, x is visible only inside function foo().

The lifetime of a variable is the period over which it exists. If x were defined without the keyword static, the lifetime would be from the entry into foo() to the return from foo(); so it would be re-initialized to 5 on every call.

The keyword static acts to extend the lifetime of a variable to the lifetime of the programme; e.g. initialization occurs once and once only and then the variable retains its value - whatever it has come to be - over all future calls to foo().

Answer from user82238 on Stack Overflow
🌐
GeeksforGeeks
geeksforgeeks.org › c language › static-variables-in-c
Static Variables in C - GeeksforGeeks
In C programming, a static variable is declared using static keyword and have the property of retaining their value between multiple function calls. It is initialized only once and is not destroyed when the function returns a value.
Published   July 23, 2025
Discussions

Why wasn't the idea of a Static variable in a function more widely adopted?
If you have a function with a static variable, then that function has one state only. If you write a function that returns a closure, you can have multiple closures, i.e. multiple states. Let's say you write a function like this: int counter() { static int count = 0; return count++; } This means you can call counter and it counts. But there is only one counter. And the call site will always look like this: int current = counter(); Let's switch to Javascript, where you can return a closure: function make_counter() { let counter = 0; return function () { return counter++; }; } Now, the calls look quite different: let counter1 = make_counter(); let counter2 = make_counter(); Now you have two counters, and you can count them independently: let count1 = counter1(); let count2 = counter2(); Does this help? More on reddit.com
🌐 r/C_Programming
38
11
June 26, 2023
Don't use a static variable inside a function in a class/struct. Is there a simpler alternative method?
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation. ... Static variables are initialized ONCE before execution starts ... More on learn.microsoft.com
🌐 learn.microsoft.com
2
0
March 13, 2022
How to make a local static variable like C has
In C you can have a local static variable inside a function that retains it's value between function calls. It is very useful for caching, here is an example pseudo-code: func call_many() { static int value; if (!… More on users.rust-lang.org
🌐 users.rust-lang.org
1
0
May 20, 2024
syntax - What does "static" mean in C? - Stack Overflow
The rest of its functions should be made static, so that the user won't be able to access them. This is encapsulation, a good practice. ... In the C programming language, static is used with global variables and functions to set their scope to the containing file. More on stackoverflow.com
🌐 stackoverflow.com
People also ask

What is a static variable in C?
A static variable in C is a variable that retains its value between function calls, ensuring it persists throughout the program's execution.
🌐
upgrad.com
upgrad.com › home › blog › software development › static variables in c explained: what they are and how to use them effectively in 2025
Static Variables in C Explained: What They Are and How to Use Them ...
Why use static variables in C?
Static variables are useful for maintaining state across multiple function calls, without the overhead of global variables.
🌐
upgrad.com
upgrad.com › home › blog › software development › static variables in c explained: what they are and how to use them effectively in 2025
Static Variables in C Explained: What They Are and How to Use Them ...
Can static variables be used in recursive functions?
Yes, static variables can be used in recursive functions to preserve state across recursive calls.
🌐
upgrad.com
upgrad.com › home › blog › software development › static variables in c explained: what they are and how to use them effectively in 2025
Static Variables in C Explained: What They Are and How to Use Them ...
🌐
Codecademy
codecademy.com › docs › static variables
C | Static Variables | Codecademy
April 28, 2025 - Static variables in C are special variables that preserve their values between function calls and throughout the program’s execution. Unlike regular variables, static variables maintain their state even after exiting the scope where they were declared.
🌐
Reddit
reddit.com › r/c_programming › why wasn't the idea of a static variable in a function more widely adopted?
r/C_Programming on Reddit: Why wasn't the idea of a Static variable in a function more widely adopted?
June 26, 2023 -

I'm not a very experienced programmer, so maybe this is a stupid question.

As far as I understand when you want to add 'statefulness' to a function in most modern languages, you'd use a closure, but often this is kinda verbose: you need to define a function inside a function and capture over a variable.

'Static' in c achieves basically the same thing as a closure, but it's way more compact (and if you are not familiar with closures, easier to reason about). Why wasn't it adopted in newer languages?

🌐
Unstop
unstop.com › home › blog › static variable in c | a comprehensive guide with coding examples
Static Variable In C | A Comprehensive Guide With Coding Examples
July 31, 2024 - A static variable in C programming is a type of variable that retains its value even after the previous scope in which it has been declared has been exited. This means that a static variable persists for the lifetime of the program, maintaining ...
🌐
TutorialsPoint
tutorialspoint.com › cprogramming › c_static_variables.htm
Static Variables in C
The compiler allocates space to ... is initialized to zero and not garbage. A static variable is not re-initialized on every function call, if it is declared inside a function....
Find elsewhere
🌐
Upgrad
upgrad.com › home › blog › software development › static variables in c explained: what they are and how to use them effectively in 2025
Static Variables in C Explained: What They Are and How to Use Them Effectively in 2025
January 7, 2025 - This ensures that the variable's lifetime spans the entire program execution, not just the function call. ... Unlike local variables, which are destroyed once a function exits, static variables live for the duration of the program, allowing you to store state information consistently...
🌐
Microchip
skills.microchip.com › fundamentals-of-the-c-programming-language-part-ii › 699889
Static Variables
A static variable is created when the program first starts and stays alive until the program exits. In an embedded system, this means that static variables always exist – the only time they disappear is when power is removed.
🌐
Scaler
scaler.com › home › topics › static variable in c
Static Variable in C - Scaler Topics
July 7, 2024 - Static variables are the variables that have the property to preserve their value from their previous scope. It means that its value does not get re-initialized every time it is declared.
🌐
Wikipedia
en.wikipedia.org › wiki › Static_variable
Static variable - Wikipedia
January 19, 2026 - The BCPL definition reads: (1) Static data items: Those data items whose extents lasts as long as the program execution time; such data items have manifest constant Lvalues. Every static data item must have been declared either in a function or routine definition, in a global declaration or as a label set by colon...
🌐
Linux Hint
linuxhint.com › static-variables-c
Static Variables in C – Linux Hint
Using the static keyword within a function call: If we want to keep the same value for a variable on every call, we may specify it as a static variable within the function. Whenever the static keyword has been used inside of a method, the re-initialization of a parameter on consecutive function ...
🌐
Code Quoi
codequoi.com › en › local-global-static-variables-in-c
Local, Global and Static Variables in C - codequoi
A static variable is by default a global variable: stored neither in the stack nor the heap, it has the same lifespan as its program. But unlike a true global variable, it has a limited scope: inside a function, it’s a global that’s only visible inside the function in which we declare it.
🌐
Quora
quora.com › Where-does-a-static-variable-inside-a-function-in-C-C++-lives
Where does a static variable inside a function in C/C++ lives? - Quora
But a static variable is stored just like a global variable: that is to say, it is allocated excactly once, in the data segement, instead of being allocated on the stack for each separate invocation of the function.
🌐
Learn C
learn-c.org › en › Static
Static - Learn C - Free Interactive C Tutorial
By default, functions are global ... to the file containing it. ... While static variables have scope over the file containing them making them accessible only inside a given file, global variables can be accessed ...
🌐
Javatpoint
javatpoint.com › static-in-c
Static in C - javatpoint
Static in C with what is c programming, C language with programming examples for beginners and professionals, control statements, c array, c pointers, c structures, c union, c strings and more.
🌐
Edureka
edureka.co › blog › static-variable-in-c
Static variable In C | How To Implement Static Variable In C | Edureka
July 21, 2020 - Static Variables Inside the Function The static variable inside the function holds the value not only till the end of the function block but till the end of the entire program. Consider the code to display static keywords function,
🌐
BYJUS
byjus.com › gate › static-variable-in-c
Syntax and Use of the Static Variable in C
April 27, 2022 - Thus, the compiler will persist with the given variable till we reach the end of our given program. One can define a static variable both- outside or inside the function. These are local to the block, and their default value is always zero.
Top answer
1 of 16
1942

Usually, you will see the static keyword in these places:

  1. A static variable inside a function keeps its value between invocations.
  2. A static global variable or function is "seen" only in the file in which it's declared.

(1) is the more foreign topic if you're a newbie, so here's an example:

#include <stdio.h>

void foo()
{
    int a = 10;
    static int sa = 10;
    
    a += 5;
    sa += 5;
    
    printf("a = %d, sa = %d\n", a, sa);
}


int main()
{
    int i;
    
    for (i = 0; i < 10; ++i)
        foo();
}

This prints:

a = 15, sa = 15
a = 15, sa = 20
a = 15, sa = 25
a = 15, sa = 30
a = 15, sa = 35
a = 15, sa = 40
a = 15, sa = 45
a = 15, sa = 50
a = 15, sa = 55
a = 15, sa = 60

This is useful for cases where a function needs to keep some state between invocations, and you don't want to use global variables. Beware, however, this feature should be used very sparingly - it makes your code not thread-safe and harder to understand.

(2) is used widely as an "access control" feature. If you have a .c file implementing some functionality, it usually exposes only a few "public" functions to users. The rest of its functions should be made static, so that the user won't be able to access them. This is encapsulation, a good practice.

Quoting Wikipedia:

In the C programming language, static is used with global variables and functions to set their scope to the containing file. In local variables, static is used to store the variable in the statically allocated memory instead of the automatically allocated memory. While the language does not dictate the implementation of either type of memory, statically allocated memory is typically reserved in data segment of the program at compile time, while the automatically allocated memory is normally implemented as a transient call stack.

See here and here for more details.

And to answer your second question, it's not like in C#.

In C++, however, static is also used to define class attributes (shared between all objects of the same class) and methods. In C there are no classes, so this feature is irrelevant.


Additionally, in C, static can be used in array declarators to specify minimum size of the array (non-function array declarators cannot use this keyword). Consider this declaration:

void func(int foo[static 42]);

The function func() takes an array of at least 42 elements.

Note that C++ does not support this use of the static keyword.

2 of 16
296

There is one more use not covered here, and that is as part of an array type declaration as an argument to a function:

int someFunction(char arg[static 10])
{
    ...
}

In this context, this specifies that arguments passed to this function must be an array of type char with at least 10 elements in it. For more info see my question here.