Tsallaka zuwa abun ciki
Justice Tech School

Scope, static and extern

Locals live inside their block. static inside a function keeps its value between calls, and static at file level hides a name from other files. extern refers to a global defined elsewhere.

Example
#include <stdio.h>
int counter(void) { static int n = 0; return ++n; }
static int hidden = 5;
int main(void) {
    printf("%d %d %d\n", counter(), counter(), counter());
    printf("%d\n", hidden);
    return 0;
}

Result

3 2 1
5

Exercise

Keep a value between calls.

 int n = 0;

Quiz Test

What does a static local do?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.