C Tutorial
C Static
static on a local persists between calls. static on a file-scope name hides it from other files.
One keyword, two jobs
Inside a function, static on a local variable means the variable is initialized once and then keeps its value across later calls. At file scope — outside every function — static means internal linkage: other .c files cannot see that name.
Same word. Different places. Different meanings. This chapter is C, not C++: there are no static class members here. StudyGrid compiles the examples as C17.
A static local keeps its value
Inside a function, static int n = 0; is initialized the first time execution reaches that line. Later calls skip the initializer and see the old value. That is how a helper can hand out 1, then 2, then 3 without a global sitting in the open.
Example
#include <stdio.h>
int next_seat(void) {
static int n = 0;
n++;
return n;
}
int main(void) {
printf("%d\n", next_seat());
printf("%d\n", next_seat());
printf("%d\n", next_seat());
return 0;
}Output is 1, then 2, then 3. The static local lives until the program ends. Only next_seat can see n.
Compile at /c/try. gcc runs this in the browser.
An ordinary local resets
Drop static and the same function prints 1 three times. Each call creates a freshn, sets it to 0, adds 1, and returns. The persistence came from static, not from the name.
Example
#include <stdio.h>
int next_seat(void) {
int n = 0;
n++;
return n;
}
int main(void) {
printf("%d\n", next_seat());
printf("%d\n", next_seat());
printf("%d\n", next_seat());
return 0;
}File-scope static is internal linkage
A name declared static at file scope is private to that translation unit — that .cfile after preprocessing. Another file that writes extern int total; will not find it. The linker treats the name as local to the first file.
This tutorial’s editor compiles one file, so you cannot watch a second file fail to link here. The program below still shows the keyword: total is visible to both functions in this file, and would be invisible to any other file.
Example
#include <stdio.h>
static int total = 0;
void add(int n) {
total += n;
}
int main(void) {
add(3);
add(4);
printf("%d\n", total);
return 0;
}Output is 7. Without static, a file-scope int total would have external linkage: other files could declare it and share the same object. static turns that sharing off.
Local static versus file-scope static
| static local | static at file scope | |
|---|---|---|
| Where you write it | Inside a function | Outside every function |
| Lifetime | Until the program ends | Until the program ends |
| Who can see it | Only that function | This file only (internal linkage) |
| Initialized | Once, on first reach | Before main starts |
Neither kind is a substitute for passing arguments. Use a static local when one function must remember a little state. Use file-scope static when several functions in the same file share data you do not want exported. Next: scope, which decides which n a line of code can see.
Worked examples
The short listings above are there so you can see the grammar. The programs here use the same statements on quantities that already have units: a speed, a pH, a count of bases. They are classroom numbers. Air resistance is ignored. g is 9.81 m/s² unless a line says otherwise.
Open them in the C editor at /c/try. Change one measurement and check whether the result still has the right unit.
Physics
How many field readings
A Hall probe might report tesla. 12 mT, 14 mT, 13 mT are three samples of a weak field (Earth’s field is ~50 µT, so this is a lab magnet). static int n inside the function counts calls. main does not own the counter.
Example
#include <stdio.h>
void log_reading(double tesla) {
static int n = 0;
n++;
printf("reading %d: %.3f T\n", n, tesla);
}
int main(void) {
log_reading(0.012);
log_reading(0.014);
log_reading(0.013);
return 0;
}Biology
Calories added across calls
120 kcal then 80 kcal is 200. static total remembers the day so far. A nutrition label’s “kcal” is a food calorie, 1000 physics calories, which is already a mess of units. The program only adds integers.
Example
#include <stdio.h>
int add_kcal(int kcal) {
static int total = 0;
total += kcal;
return total;
}
int main(void) {
printf("%d\n", add_kcal(120));
printf("%d\n", add_kcal(80));
return 0;
}