Skip to content
doubtly

2 answers

The scope of a variable is the region of the program in which the variable is visible and can be used. In C, variables mainly have block (local) scope or file (global) scope.

  • Block (local) scope: A variable declared inside a block { }, for example inside a function or a loop, can be used only from its declaration to the end of that block. Function parameters also have block scope (the function body).
  • File (global) scope: A variable declared outside all functions can be used from its declaration to the end of the source file, by every function that follows. With extern it can also be used in other files; declaring it static keeps it private to that file.
  • Function scope: applies only to labels used with goto; a label is visible anywhere in its function.
  • Function prototype scope: parameter names in a prototype, e.g. int add(int a, int b);, are visible only up to the end of that prototype.
int count = 0;          /* file (global) scope */

void show() {
    int x = 5;          /* block (local) scope: only inside show() */
    count++;            /* global variable is visible here */
}

C uses lexical (static) scoping: whether a name is visible is decided by where it is written in the program text. If a local variable has the same name as a global one, the local variable hides the global one inside its block.

Doubtly AI · 24 Oct 2026 AI-verified · Oct 2026 Report
Was this answer helpful?

Your answer

Log in or create a free account to answer this question.