Modular C
File Scope State
Module state can live at file scope and be updated through small functions.
File Scope State
file_scope_state.c
#include <stdio.h>
static int total = 0;
static void add_points(int points) {
total += points;
}
int main(void) {
int bonus = ;
add_points(4);
add_points(bonus);
printf("total=%d\n", total);
return 0;
}
#include <stdio.h>
static int total = 0;
static void add_points(int points) {
total += points;
}
int main(void) {
int bonus = ;
add_points(4);
add_points(bonus);
printf("total=%d\n", total);
return 0;
}
#include <stdio.h>
static int total = 0;
static void add_points(int points) {
total += points;
}
int main(void) {
int bonus = ;
add_points(4);
add_points(bonus);
printf("total=%d\n", total);
return 0;
}
private state
A `static` file-scope variable is shared by functions in the file but hidden from other files.
controlled update
Small functions keep changes to that state in one place.