ZetCode

K&R C scope and storage classes

last modified December 5, 2026 K&R C 1978

Every variable in C has a scope, the part of the program where its name is known, and a storage class, which decides how long the variable lives. K&R C has four storage classes: auto, extern, static, and register. This article shows each of them in a single file; the program structure article spreads programs over several files.

Automatic variables

A variable declared inside a function is local to that function. It is created when the function is called and disappears when the function returns, so it is called an automatic variable. The keyword auto states this explicitly, but it is the default and is almost never written.

automatic.c
#include <stdio.h>

int count_down(n)
int n;
{
    int i;

    for (i = n; i > 0; i--)
        printf("%d ", i);
    printf("\n");
    return 0;
}

int main()
{
    auto int i;

    for (i = 1; i <= 4; i++)
        count_down(i);
    printf("i in main is %d\n", i);
    return 0;
}

Both functions have a variable called i, and the two have nothing in common: count_down counts its own i down while the loop in main counts its i up. Each call of count_down gets a fresh i. An automatic variable does not keep its value between calls and starts with an undefined value unless it is initialized.

$ gcc -std=c89 -pedantic -Wall -Wextra -o automatic automatic.c
$ ./automatic
1 
2 1 
3 2 1 
4 3 2 1 
i in main is 5

External variables

A variable defined outside of all functions is an external variable. It exists for the whole run of the program, and every function defined after it in the file can use it by name. External variables let functions share data without passing it as arguments.

dice.c
#include <stdio.h>

unsigned seed = 1;

int set_seed(s)
unsigned s;
{
    seed = s;
    return 0;
}

int roll()
{
    seed = seed * 1664525 + 1013904223;
    return (seed >> 16) % 6 + 1;
}

int main()
{
    int i;

    for (i = 0; i < 10; i++)
        printf("%d ", roll());
    printf("\nseed is now %u\n", seed);

    set_seed(1);
    for (i = 0; i < 10; i++)
        printf("%d ", roll());
    printf("\n");
    return 0;
}

seed is shared by set_seed and roll, and main prints it too. roll is a linear congruential generator: it multiplies the seed, adds a constant, and keeps the low 32 bits, because unsigned arithmetic wraps. It returns a die roll from the higher bits. Resetting the seed to 1 repeats the same sequence.

The book warns against relying on external variables too much: any function can change them, which makes the flow of data hard to follow. An external variable declared static is visible only in its own source file, which hides a variable like seed from other files.

$ gcc -std=c89 -pedantic -Wall -Wextra -o dice dice.c
$ ./dice
5 3 5 2 1 1 3 1 1 2 
seed is now 2745540835
5 3 5 2 1 1 3 1 1 2 

On the PDP-11 an unsigned has 16 bits. The seed keeps only the low 16 bits of each product, so seed >> 16 is always 0 and every roll is 1. A generator for the PDP-11 needs constants chosen for 16 bits and must take the roll from the high bits of a 16-bit seed.

$ cc dice.c
$ a.out
1 1 1 1 1 1 1 1 1 1 
seed is now 41187
1 1 1 1 1 1 1 1 1 1 

extern declarations

A name is known only from the point where it is declared. A function that uses an external variable defined later in the file, or in another file, needs an extern declaration. The definition creates the variable and sets aside storage; an extern declaration only announces its type.

totals.c
#include <stdio.h>

int add(n)
int n;
{
    extern int total, count;

    total += n;
    count++;
    return total;
}

int main()
{
    extern int total, count;
    int i;

    for (i = 1; i <= 4; i++)
        printf("running total: %d\n", add(i * 10));
    printf("%d numbers, total %d\n", count, total);
    return 0;
}

int total = 0;
int count = 0;

total and count are defined at the end of the file. The extern lines inside add and main make them known in these functions, in the style of the book. A program has exactly one definition of each external variable, but may have any number of extern declarations of it. Placing the definitions at the top of the file, as in the previous example, makes the declarations unnecessary.

$ gcc -std=c89 -pedantic -Wall -Wextra -o totals totals.c
$ ./totals
running total: 10
running total: 30
running total: 60
running total: 100
4 numbers, total 100

Static local variables

A local variable declared static keeps its value between calls. It is created once, before the program starts, and its initializer is applied only once. Its name is still local to the function.

tickets.c
#include <stdio.h>

int next_ticket()
{
    static int ticket = 100;
    int calls = 0;

    ticket++;
    calls++;
    printf("ticket %d, automatic counter %d\n", ticket, calls);
    return ticket;
}

int main()
{
    int last;

    next_ticket();
    next_ticket();
    last = next_ticket();
    printf("last ticket: %d\n", last);
    return 0;
}

ticket starts at 100 and grows with every call, because it is not created anew. calls is automatic, so it is set to 0 on every call and never gets past 1. Static locals serve the same purpose as external variables, a value that survives between calls, without making the value visible to other functions.

$ gcc -std=c89 -pedantic -Wall -Wextra -o tickets tickets.c
$ ./tickets
ticket 101, automatic counter 1
ticket 102, automatic counter 1
ticket 103, automatic counter 1
last ticket: 103

Register variables

A register declaration tells the compiler that a variable is used heavily and should be kept in a machine register. It may be applied to automatic variables and to parameters. The address of a register variable cannot be taken, because a register has no address.

fast_sum.c
#include <stdio.h>

#define LIMIT 1000

int sum_of_squares(n)
register int n;
{
    register int i;
    long sum;

    sum = 0;
    for (i = 1; i <= n; i++)
        sum += (long) i * i;
    printf("sum of squares up to %d: %ld\n", n, sum);
    return 0;
}

int main()
{
    sum_of_squares(10);
    sum_of_squares(LIMIT);
    return 0;
}

The loop counter and the parameter are declared register. In 1978, when the compiler did little optimization, this made loops measurably faster, and only a few variables of each function could be placed in registers; the rest of the declarations were ignored. GCC chooses registers better than the programmer and ignores the hint; for GCC, register is only a promise not to take the variable's address. The sum is a long, and the cast makes the multiplication long too.

$ gcc -std=c89 -pedantic -Wall -Wextra -o fast_sum fast_sum.c
$ ./fast_sum
sum of squares up to 10: 385
sum of squares up to 1000: 333833500

Block structure

C does not let functions be defined inside other functions, but any block, a group of statements in braces, may begin with declarations. A variable declared in a block is local to that block and hides any variable of the same name declared outside it.

blocks.c
#include <stdio.h>

int level = 0;

int main()
{
    int x, y;

    x = 1;
    y = 2;
    printf("level %d, x = %d\n", level, x);
    {
        int x, level;

        x = 10;
        level = 1;
        printf("level %d, x = %d, y = %d\n", level, x, y);
    }
    printf("level %d, x = %d\n", level, x);

    if (x < y) {
        int t;

        t = x;
        x = y;
        y = t;
    }
    printf("after the swap: x = %d, y = %d\n", x, y);
    return 0;
}

The inner block declares its own x and level, which hide the local x of main and the external level. After the block ends, the outer variables are visible again with their old values. The temporary t of the swap lives only inside the if block. Reusing names this way is legal but confusing, and GCC can warn about it with -Wshadow.

$ gcc -std=c89 -pedantic -Wall -Wextra -o blocks blocks.c
$ ./blocks
level 0, x = 1
level 1, x = 10, y = 2
level 0, x = 1
after the swap: x = 2, y = 1

Initialization

External and static variables are initialized only once, and to zero when the program gives no initializer. Automatic and register variables are initialized every time the function or block is entered, and they hold garbage when they have no initializer.

initialization.c
#include <stdio.h>

#define BASE 3

int calls;
int limit = BASE * 4;

int visit(n)
int n;
{
    static int total;
    int doubled = 2 * n;

    calls++;
    total += doubled;
    printf("visit %d: doubled %d, total %d\n", calls, doubled, total);
    return 0;
}

int main()
{
    printf("calls = %d, limit = %d\n", calls, limit);
    visit(1);
    visit(5);
    visit(limit);
    return 0;
}

calls and total have no initializers and start at 0. The initializer of an external or static variable must be a constant expression, such as BASE * 4. An automatic variable may be initialized with any expression: doubled is computed from the parameter on each call. Automatic arrays and structures cannot be initialized at all; the arrays article shows how to fill them.

$ gcc -std=c89 -pedantic -Wall -Wextra -o initialization initialization.c
$ ./initialization
calls = 0, limit = 12
visit 1: doubled 2, total 2
visit 2: doubled 10, total 12
visit 3: doubled 24, total 36

Constant initializers

External variables are initialized before the program runs, when no code has executed yet. An expression that needs the value of another variable cannot be computed at that time, so it cannot initialize an external variable.

not_constant.c
#include <stdio.h>

int base = 10;
int limit = base * 2;

int main()
{
    printf("%d %d\n", base, limit);
    return 0;
}

base is a variable, so base * 2 is not a constant expression, and GCC rejects it. A #define constant in place of base would work, as would an assignment at the start of main.

$ gcc -std=c89 -pedantic -Wall -Wextra -o not_constant not_constant.c
not_constant.c:4:13: error: initializer element is not constant
    4 | int limit = base * 2;
      |             ^~~~

Source

Dennis Ritchie: C Reference Manual (1975)

GCC: Options to Request or Suppress Warnings

In this article we have covered the storage classes of K&R C: automatic and external variables, extern declarations, static local variables, register variables, block structure, and the rules for initialization. The next article, K&R C character input and output, starts the classic programs of the book. The modern rules are described in C scope.

Author

I am Jan Bodnar, a programmer and technical writer. I have been writing programming tutorials at ZetCode since 2007 and have published eight e-books. See about the author.

List all K&R C tutorials.