Static Variable in C

Level 4113 keystrokes

Uses a static variable to preserve value between function calls.

C code

// Function with static variable
1void counter() {
// The static variable is initialized only once
// and keeps its value between calls
2 static int count = 0;
// Increment the counter
3 count++;
// Print current count
4 printf("Count: %d\n", count);
5}
6
// First call: count becomes 1
7counter();
// Second call: count becomes 2
8counter();
// Third call: count becomes 3
9counter();

Output

Count: 1
Count: 2
Count: 3
Practice typing this code

Practice typing this code. Results are not recorded in the rankings.

Symbols in this code

The symbols you type in this C code and how often they appear.

  • ;× 6
  • (× 5
  • )× 5
  • "× 2
  • +× 2
  • ,× 1
  • :× 1
  • {× 1
  • }× 1
  • \× 1

More C examples at level 4

← C typing practice