Static Keyword in C
The static keyword gives variables memory that persists between function calls. Learn how static creates "sticky" variables and private globals.
What You Will Learn
- ✓Create static local variables that remember values
- ✓Make global variables private to a file
- ✓Understand when to use static
01What is the Static Keyword?
🎯 Simple Definition
The static keyword in C has two main uses:
1. Static Variables
Keep value between function calls
2. Static Functions
Limit visibility to current file
02Static Variables: Remember Value Between Calls
🤔 The Problem: Normal Local Variables
Normal local variables are destroyed when a function ends. Each time you call the function, the variable starts fresh.
void counter() {
int count = 0;
count++;
printf("%d", count);
}
✅ The Solution: Static Variables
Static variables persist — they keep their value between function calls!
void counter() {
static int count = 0;
count++;
printf("%d ", count);
}
📝 This program demonstrates the difference between normal and static variables.
1#include <stdio.h>23// Function with NORMAL variable4void normalCounter() {5 int count = 0; // Created fresh each call6 count++;7 printf("Normal: %d\n", count);8}910// Function with STATIC variable11void staticCounter() {12 static int count = 0; // Initialized ONCE, persists!13 count++;14 printf("Static: %d\n", count);15}1617int main() {18 printf("=== Normal Variable (resets) ===\n");19 normalCounter(); // Prints 120 normalCounter(); // Prints 121 normalCounter(); // Prints 122 23 printf("\n=== Static Variable (persists) ===\n");24 staticCounter(); // Prints 125 staticCounter(); // Prints 226 staticCounter(); // Prints 327 28 return 0;29}=== Normal Variable (resets) ===
Normal: 1
Normal: 1
Normal: 1
=== Static Variable (persists) ===
Static: 1
Static: 2
Static: 3
| Feature | Normal Local Variable | Static Variable |
|---|---|---|
| Storage | Stack | Data segment |
| Lifetime | Function call only | Entire program |
| Initialization | Every call | Only once |
| Default value | Garbage | Zero |
03Static Global Variables: File-Level Privacy
When used with global variables, staticlimits the variable's visibility to the current file only.
Normal global (accessible everywhere)
int count = 0;
Static global (this file only)
static int count = 0;
1// file1.c2static int privateCounter = 0; // Only visible in file1.c3int publicCounter = 0; // Visible everywhere45void incrementPrivate() {6 privateCounter++;7}89// file2.c10extern int publicCounter; // Can access this ✓11// extern int privateCounter; // ERROR! Cannot access static variable ✗04Static Functions: Private to File
A static function can only be called from within the same file. It's like making a function "private".
1#include <stdio.h>23// Static function - only visible in this file4static void helperFunction() {5 printf("I'm a private helper!\n");6}78// Normal function - can be called from other files9void publicFunction() {10 printf("I'm public!\n");11 helperFunction(); // Can call static function from same file12}1314int main() {15 publicFunction(); // Works ✓16 helperFunction(); // Works (same file) ✓17 return 0;18}💡 Why Use Static Functions?
- • Encapsulation: Hide implementation details
- • No name conflicts: Different files can have same function name
- • Optimization: Compiler can inline static functions
05Common Use Cases
📊 Counters & Statistics
int getNextID() {
static int id = 0;
return ++id;
}
🔧 One-time Initialization
void init() {
static int done = 0;
if (done) return;
done = 1;
}
📦 Caching Results
int fibonacci(int n) {
static int cache[100];
if (cache[n]) return cache[n];
}
🔒 State Machines
void stateMachine() {
static int state = 0;
switch(state) {
case 0: state = 1; break;
case 1: state = 2; break;
}
}
06Summary
🎯 Key Takeaways
- •Static local variable: Persists between function calls
- •Static global variable: Visible only in current file
- •Static function: Callable only from current file
- •Initialization: Static variables initialized only once
- •Default value: Static variables default to 0
07Next Steps
Learn more about storage classes and variable lifetime: