typestar

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

  1. A static local keeps its value between calls.
  2. A static function or global is private to its translation unit.
  3. extern declares something defined in another file.

Keywords and builtins used here

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.

Type this snippet

Step 2 of 7 in Functions, step 16 of 35 in Language basics.

← Previous Next →