Storage classes in C
static, extern and automatic: where a variable lives and how long it lasts.
static int call_count = 0;
static int next_id(void) {
static int id = 100;
call_count++;
return id++;
}
int ids_taken(void) {
return call_count;
}
int take_three(int *out) {
for (int i = 0; i < 3; i++) {
out[i] = next_id();
}
return ids_taken();
}
How it works
- A
staticlocal keeps its value between calls. - A
staticfunction or global is private to its translation unit. externdeclares something defined in another file.
Keywords and builtins used here
forids_takenintnext_idreturnstatictake_threevoid
The run, in numbers
- Lines
- 18
- Characters to type
- 250
- Tokens
- 79
- Three-star pace
- 90 tpm
At the three-star pace of 90 tokens a minute, this run takes about 53 seconds.
Step 2 of 7 in Functions, step 16 of 35 in Language basics.