The Wayback Machine - https://web.archive.org/web/20260201010413/https://www.geeksforgeeks.org/dsa/static-and-dynamic-scoping/

Static and Dynamic Scoping

Last Updated : 7 Jan, 2026

The scope of a variable x in the region of the program in which the use of x refers to its declaration. One of the basic reasons for scoping is to keep variables in different parts of the program distinct from one another. Since there are only a small number of short variable names, and programmers share habits about naming of variables (e.g., I for an array index), in any program of moderate size the same variable name will be used in multiple different scopes.
Scoping is generally divided into two classes: 
1. Static Scoping 
2. Dynamic Scoping

Static Scoping: 

Static scoping is also called lexical scoping. In this scoping, a variable always refers to its top-level environment. This is a property of the program text and is unrelated to the run-time call stack. Static scoping also makes it much easier to make a modular code as a programmer can figure out the scope just by looking at the code. In contrast, dynamic scope requires the programmer to anticipate all possible dynamic contexts.
In most programming languages including C, C++, and Java, variables are always statically (or lexically) scoped i.e., binding of a variable can be determined by program text and is independent of the run-time function call stack. 
For example, the output for the below program is 10, i.e., the value returned by f() is not dependent on who is calling it (Like g() calls it and has a x with value 20). f() always returns the value of global variable x.
 

C
// A C program to demonstrate static scoping.
#include<stdio.h>
int x = 10;

// Called by g()
int f()
{
   return x;
}

// g() has its own variable
// named as x and calls f()
int g()
{
   int x = 20;
   return f();
}

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

Output : 

10


To sum up, in static scoping the compiler first searches in the current block, then in global variables, then in successively smaller scopes.

Dynamic Scoping: 

With dynamic scope, a global identifier refers to the identifier associated with the most recent environment and is uncommon in modern languages. In technical terms, this means that each identifier has a global stack of bindings and the occurrence of an identifier is searched in the most recent binding. 
In simpler terms, in dynamic scoping, the compiler first searches the current block and then successively all the calling functions.
 

C
// Since dynamic scoping is very uncommon in 
// the familiar languages, we consider the 
// following pseudo code as our example. It
// prints 20 in a language that uses dynamic
// scoping.   

int x = 10;

// Called by g()
int f()
{
   return x;
}

// g() has its own variable
// named as x and calls f()
int g()
{
   int x = 20;
   return f();
}

main()
{
  printf(g());
}

 
Output in a language that uses Dynamic Scoping : 
 

20

Static Vs Dynamic Scoping 

AspectStatic Scoping (Lexical Scoping)Dynamic Scoping
Scope DeterminationThe scope of a variable is determined by its position in the source code, independent of how the program executes.The scope of a variable is determined by the order of function calls during program execution.
Time of BindingVariable bindings are fixed at compile time, allowing the compiler to know exactly which variable is being referenced.Variable bindings are resolved at runtime based on the current call stack.
Variable LookupThe compiler searches for variables in enclosing lexical blocks following the program’s textual structure.The runtime system searches for variables in the chain of active function calls.
PredictabilityProgram behavior is predictable because variable access can be understood by reading the source code alone.Program behavior is harder to predict since it depends on the execution path taken at runtime.
PerformanceAccess to variables is faster because their locations are determined during compilation.Access to variables is slower due to runtime stack traversal to resolve bindings.
Compiler OptimizationEnables optimizations such as register allocation, inlining, and dead code elimination.Limits compiler optimizations because variable bindings are not known in advance.
SafetyPrevents accidental access or modification of variables outside their intended lexical scope.Increases the possibility of unintended variable modification from unrelated functions.
DebuggingDebugging is simpler due to fixed and well-defined scope boundaries.Debugging becomes complex as variable bindings may change during execution.
Language SupportSupported by most modern programming languages such as C, C++, Java, and Python.Rarely supported; seen in limited form in languages like Perl using local.
Use in ClosuresFully supports closures and nested functions with well-defined environments.Does not support closures because bindings are determined dynamically.
Comment