Chapter 13Beginner

Static Keyword in C

The static keyword gives variables memory that persists between function calls. Learn how static creates "sticky" variables and private globals.

12 min readUpdated 2024-12-16
staticstatic variablestatic functionpersistencefile scopelocal static

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.

static_variable.c
C
1#include <stdio.h>
2
3// Function with NORMAL variable
4void normalCounter() {
5 int count = 0; // Created fresh each call
6 count++;
7 printf("Normal: %d\n", count);
8}
9
10// Function with STATIC variable
11void staticCounter() {
12 static int count = 0; // Initialized ONCE, persists!
13 count++;
14 printf("Static: %d\n", count);
15}
16
17int main() {
18 printf("=== Normal Variable (resets) ===\n");
19 normalCounter(); // Prints 1
20 normalCounter(); // Prints 1
21 normalCounter(); // Prints 1
22
23 printf("\n=== Static Variable (persists) ===\n");
24 staticCounter(); // Prints 1
25 staticCounter(); // Prints 2
26 staticCounter(); // Prints 3
27
28 return 0;
29}
Output

=== Normal Variable (resets) ===

Normal: 1

Normal: 1

Normal: 1

=== Static Variable (persists) ===

Static: 1

Static: 2

Static: 3

FeatureNormal Local VariableStatic Variable
StorageStackData segment
LifetimeFunction call onlyEntire program
InitializationEvery callOnly once
Default valueGarbageZero

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;

static_global.c
C
1// file1.c
2static int privateCounter = 0; // Only visible in file1.c
3int publicCounter = 0; // Visible everywhere
4
5void incrementPrivate() {
6 privateCounter++;
7}
8
9// file2.c
10extern 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".

static_function.c
C
1#include <stdio.h>
2
3// Static function - only visible in this file
4static void helperFunction() {
5 printf("I'm a private helper!\n");
6}
7
8// Normal function - can be called from other files
9void publicFunction() {
10 printf("I'm public!\n");
11 helperFunction(); // Can call static function from same file
12}
13
14int 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: