Skip to content
Tutorials Center
My learning

Topics

C Reference ยท Functions and Scope

Scope and lifetime in C

Reference for scope and lifetime in C: block, file, function and prototype scope, name shadowing, name spaces and how scope differs from object lifetime.

Scope is the part of the source code where a name is visible. Lifetime is the part of the program's execution during which an object exists. They are related but different: a variable can be alive and still out of scope (a static local while another function runs), and a pointer can be in scope but point at an object whose lifetime has ended.

C has four kinds of scope: file, block, function prototype and function (only for labels). Most declarations you write are either at file scope, outside every function, or at block scope, inside braces.

C
int global = 1;            /* file scope */
void f(int p) {            /* p: block scope of the function body */
    int local = 2;         /* block scope */
    { int inner = 3; }     /* inner scope ends at the brace */
}

Try it

C Block scope and shadowing

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

C Lifetime, scope and labels

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

Syntax

There is no syntax for scope itself; it follows from where a declaration appears.

Scope Where declared Visible
File Outside any function or block From the declaration to the end of the translation unit.
Block Inside { }, including function bodies, loop headers and parameters From the declaration to the end of the block.
Function prototype In the parameter list of a declaration that is not a definition Until the end of the prototype.
Function Labels only (name:) The whole function, even before the label appears.

Lifetime categories (see Storage duration): automatic (until the block ends), static (the whole run), thread, and allocated (until free).

Errors / undefined behavior: using a name outside its scope is a compile error. Using an object after its lifetime ended, for example through a pointer to a local of a returned function, is undefined behavior.

Usage notes

  • Shadowing. An inner declaration hides an outer one with the same name. It is legal but confusing; compile with -Wshadow to catch accidents.
  • Point of declaration. A name's scope begins right after its declarator, so int x = x; initializes x with itself (indeterminate). A variable is not visible above its declaration.
  • Declare late and small. C99 and later allow declarations anywhere in a block and in for headers. Narrow scopes mean fewer bugs.
  • Jumping into a scope with goto or switch can skip initializers; this is allowed but leaves the variable indeterminate, and is an error for variably modified types.
  • Name spaces. Labels, struct/union/enum tags, members and ordinary identifiers have separate name spaces, so struct item { int item; } works. Each struct also has its own member space.
  • File scope and linkage. A file-scope name can additionally have internal or external linkage, which controls visibility between files; see Linkage.
  • Macros ignore scope. The preprocessor works on text, so #define names are visible from the definition to the end of the file (or #undef).
  • Parameters have the scope of the function body's outermost block, so a local with the same name in that same block is a redeclaration error.
  • Structure and enum constants declared inside a function are local to that block, but constants of an enum declared at file scope are visible everywhere after it.