Static Variable in C
Level 5144 keystrokes
Uses a static local variable to keep a count between calls: it is initialized only once, so each call to counter() returns one more than the last. Three calls print 1, 2 and 3.
C code
// A function that remembers how many times it has been called1int counter() { // static: initialized to 0 only once and kept between calls, unlike a normal local2 static int count = 0; // Increases the stored count by one3 count++; // Returns the updated count4 return count;5}6 // First call: count goes from 0 to 1, prints 17printf("%d\n", counter());// Second call: the kept value grows to 28printf("%d\n", counter());// Third call: prints 39printf("%d\n", counter());Output
1 2 3
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.
(× 7)× 7;× 6"× 6,× 3\× 3%× 3+× 2{× 1}× 1