Question
I have seen the keyword static used in different places in C code. What does static mean in C, and is it similar to a static method or class member in C# where one implementation is shared across objects?
Short Answer
By the end of this page, you will understand that static in C does not mean the same thing it usually means in C#. In C, static mainly affects lifetime and visibility. You will learn how static behaves for local variables, global variables, and functions, and how developers use it in real C programs.
Concept
In C, the keyword static changes meaning depending on where it is used.
The two main ideas are:
- Storage duration: how long something exists in memory
- Linkage: whether a name can be accessed from other source files
1. static for a local variable inside a function
A normal local variable is created when the function starts and destroyed when the function ends.
A static local variable is different:
- it is created only once
- it keeps its value between function calls
- it is still only visible inside that function
Example:
#include <stdio.h>
void counter(void) {
static int count = 0;
count++;
printf("count = %d\n", count);
}
int main(void) {
counter();
counter();
counter();
return 0;
}
Output:
count = 1
count = 2
count = 3
If count were not , it would reset to every time runs.
Mental Model
Think of static in C like a private storage room or a locked office drawer.
- A normal local variable is like a sticky note you write during a meeting and throw away when the meeting ends.
- A static local variable is like a notebook kept in the room. Each time you come back, the old notes are still there.
- A static global variable or static function is like something kept in a locked office that only people in that office can use. Other offices cannot access it.
So the easiest mental model is:
- inside a function:
staticmeans “keep this value between calls” - outside a function:
staticmeans “keep this name private to this file”
Syntax and Examples
Core syntax
Static local variable
void func(void) {
static int x = 0;
x++;
}
Static global variable
static int total_users = 0;
Static function
static void log_message(const char *msg) {
/* ... */
}
Example 1: remembering state across calls
#include <stdio.h>
void next_id(void) {
static int id = 1000;
printf("Next id: %d\n", id);
id++;
}
int main() {
next_id();
next_id();
next_id();
;
}
Step by Step Execution
Consider this program:
#include <stdio.h>
void visit(void) {
static int visits = 0;
visits++;
printf("visits = %d\n", visits);
}
int main(void) {
visit();
visit();
return 0;
}
Step-by-step
- The program starts.
- The
static int visits = 0;variable is allocated in static storage, not on the regular function stack. main()callsvisit()for the first time.visitsalready exists and starts at0.visits++changes it to1.- The program prints
visits = 1. visit()returns, butvisitsis .
Real World Use Cases
Where static is used in real programs
1. Private helper functions in a .c file
A file may contain support functions that should not be visible elsewhere.
static int parse_digit(char c) {
return c - '0';
}
2. Internal module state
A source file may keep internal counters, caches, or flags.
static int initialized = 0;
This avoids exposing internal details to the rest of the program.
3. Simple counters across function calls
Useful for IDs, call counts, retry tracking, or statistics.
void log_event(void) {
static unsigned count = 0;
count++;
}
4. One-time initialization logic
A function may need setup work only once.
Real Codebase Usage
In real C codebases, static is heavily used to keep modules clean and reduce accidental coupling.
Common patterns
File-private helper functions
static int is_valid_port(int port) {
return port >= 1 && port <= 65535;
}
This prevents other files from depending on internal helpers.
File-private configuration or state
static int debug_enabled = 0;
This keeps implementation details hidden inside one module.
Guarded initialization
void init_logger(void) {
static int initialized = 0;
if (initialized) {
return;
}
initialized = 1;
/* setup code */
}
This is a guard-clause style use of a static local variable.
Limiting exported symbols
Common Mistakes
1. Assuming static means the same as in C# or Java
In C, static is not about class members or shared methods on objects.
Avoid this mistake
Think in terms of:
- storage duration
- file visibility
2. Expecting a static local variable to reset every call
Broken assumption:
void test(void) {
static int x = 0;
x++;
printf("%d\n", x);
}
A beginner may expect this to always print 1, but it prints 1, then 2, then 3.
3. Trying to call a static function from another file
static void helper(void) {
}
If another .c file tries to call , it will fail because the function is only visible in its own file.
Comparisons
static in different places in C
| Location | What static affects | Result |
|---|---|---|
| Inside a function | Storage duration | Variable keeps its value between calls |
| File-scope variable | Linkage | Variable is visible only in that source file |
| File-scope function | Linkage | Function is callable only in that source file |
C static vs C# static
| Feature | C | C# |
|---|---|---|
| Object-oriented meaning | No | Yes |
| Associated with classes/objects |
Cheat Sheet
Quick reference
Static local variable
void func(void) {
static int x = 0;
}
- Scope: inside the function only
- Lifetime: entire program
- Value persists between function calls
Static file-scope variable
static int count = 0;
- Scope: this source file
- Linkage: internal
- Other files cannot access it with
extern
Static function
static void helper(void) {
}
- Callable only inside the same
.cfile - Useful for private helper functions
Key rule
static in C usually means one of these:
- keep this value alive for the whole program
FAQ
Is static in C the same as static in C#?
No. In C, static mainly controls lifetime and file visibility. In C#, static usually means a member belongs to the type rather than an instance.
What happens when a local variable is declared static in C?
It keeps its value between function calls and exists for the lifetime of the program.
Can a static function in C be called from another file?
No. A static function at file scope has internal linkage, so it is only visible inside its own .c file.
Can a static global variable be accessed with extern from another file?
No. A file-scope static variable is private to that source file.
Does a static local variable become global?
No. Its lifetime is long like a global, but its scope is still limited to the function where it is declared.
When should I use static on a function in C?
Use it when the function is an internal helper and should not be part of the module's public API.
Is static useful for encapsulation in C?
Mini Project
Description
Build a small C module that tracks how many times a helper function is called while keeping internal details private to one source file. This demonstrates both major uses of static in C: persistent local state and file-private functions.
Goal
Create a program where a public function prints a processed value and internally counts how many times processing has happened, without exposing the helper function or the counter to other files.
Requirements
- Create one public function that can be called from
main - Use a
statichelper function that is private to the source file - Use a
staticlocal variable to count how many times processing has occurred - Print the processed result and the call count each time the function runs
Keep learning
Related questions
Building More Fault-Tolerant Embedded C++ Applications for Radiation-Prone ARM Systems
Learn practical C++ and compile-time techniques to reduce soft-error damage in embedded ARM systems exposed to radiation.
Definition vs Declaration in C and C++: What’s the Difference?
Learn the difference between declarations and definitions in C and C++ with simple examples, common mistakes, and practical usage.
Difference Between #include <...> and #include "..." in C and C++
Learn the difference between #include with angle brackets and quotes in C and C++, including search paths, examples, and common mistakes.