TExcelTAD
Home › C › C Scope

C Scope

The scope of a variable is the part of the program where its name can be used. A variable declared in one place is not automatically visible everywhere. Scope rules keep different parts of a program from accidentally interfering with each other, and they let you reuse simple names like i or count in many functions.

C has two main kinds of scope for variables: local (block) scope and global (file) scope.

Local scope

A variable declared inside a function — or inside any { } block — is local to that block. It exists only from its declaration to the closing brace, and code outside cannot see it:

c · try it yourself
#include <stdio.h>

void show_secret(void) {
    int secret = 42;            // local to show_secret
    printf("secret = %d\n", secret);
}

int main(void) {
    show_secret();
    // printf("%d\n", secret);  // error: 'secret' undeclared here
    int secret = 7;             // a completely different variable
    printf("main's secret = %d\n", secret);
    return 0;
}
secret = 42
main's secret = 7

The two secret variables never meet: each lives in its own function. Function parameters are local variables too.

Local variables are also temporary: each call to a function creates fresh copies, and they are destroyed when the function returns. A local variable does not remember its value between calls (unless it is declared static — see C Storage Classes).

Block scope

Any pair of braces starts a new scope — not just functions. Variables declared in an if body or a for loop exist only inside it:

c · try it yourself
#include <stdio.h>

int main(void) {
    int total = 0;
    for (int i = 1; i <= 3; i++) {
        int doubled = i * 2;      // new variable for each pass
        total += doubled;
    }
    // i and doubled no longer exist here
    printf("total = %d\n", total);

    if (total > 10) {
        int bonus = 5;
        total += bonus;
    }
    printf("total after bonus = %d\n", total);
    return 0;
}
total = 12
total after bonus = 17

Tip: Declare variables in the smallest scope that works. It makes code easier to read and prevents a variable from being misused far away from where it belongs.

Global scope

A variable declared outside all functions is global. It is visible from its declaration to the end of the file, in every function, and it exists for the entire run of the program:

c · try it yourself
#include <stdio.h>

int visits = 0;          // global variable

void visit(void) {
    visits++;
}

int main(void) {
    visit();
    visit();
    visit();
    printf("visits = %d\n", visits);
    return 0;
}
visits = 3

Unlike local variables, globals are automatically initialised to zero if you do not give them a value.

Shadowing

If an inner scope declares a variable with the same name as an outer one, the inner one shadows (hides) the outer one until the inner block ends:

c · try it yourself
#include <stdio.h>

int level = 1;                       // global

int main(void) {
    printf("A: level = %d\n", level);    // global
    int level = 2;                       // local, shadows the global
    printf("B: level = %d\n", level);
    {
        int level = 3;                   // shadows main's level
        printf("C: level = %d\n", level);
    }
    printf("D: level = %d\n", level);    // main's level again
    return 0;
}
A: level = 1
B: level = 2
C: level = 3
D: level = 2

Shadowing is legal but confusing. GCC can warn about it with -Wshadow.

Why globals are risky

Local variables Global variables
visible only in their block visible in every function of the file (and other files with extern)
created and destroyed with each call live for the whole program
garbage until initialised automatically zero
easy to reason about any function might change them at any time

Globals make it hard to know which function changed a value, and they get in the way of reusing functions. Prefer passing values as parameters and returning results. Reserve globals for truly program-wide settings — and consider making them const or static.

Summary

  • A name is visible from its declaration to the end of the enclosing block.
  • Parameters behave like local variables of the function.
  • Globals are declared outside functions and live for the whole program.
  • An inner declaration with the same name shadows an outer one.

Test yourself

Where can a variable declared inside a for loop's body be used?

  1. Anywhere in the file
  2. Anywhere in the function
  3. Only inside that loop body
  4. Only in main

What is the initial value of a global int declared without an initialiser?

  1. Garbage
  2. 0
  3. 1
  4. -1

What is shadowing?

  1. Copying a variable
  2. An inner variable with the same name hides an outer one
  3. Deleting a global
  4. Passing a pointer