Scope Rules in C

Updated

The scope of a variable x is the region of the program in which uses of x refers to its declaration. In C, all identifiers are lexically(or statically) scoped.

Scoping in C is generally divided into two classes: Static Scoping, and Dynamic scoping.

Static Scoping :

Static scoping is also called lexical scoping. Static scope refers to the scope of variable that is defined at compile time. It is always refers to the variable with top level environment. Static scoping also makes it much easier to make a modular code as programmer can figure out the scope just by looking at the code. Binding of a variable can be determined by program text and is independent of the run-time function call stack.

Dynamic Scope :

With dynamic scope, a global identifier refers to the identifier associated with the most recent environment, and is uncommon in modern languages. Dynamic scope refers to scope of a variable that is defined at run time. It refers to the identifier with the most recent environment.

When a variable is referred in program we should follow the following priority to associate its value

In static scoping we can associate scope of the variable during compile time. And in Dynamic scoping at run time.

Example –

#include<stdio.h>

// global variable
int y = 5;

int fun1() {
    
    // it accesses global variable y
   int x = y + 5;
   printf("%d\n",x);
}

int fun2() {
    
    // local variable
   int y = 2;
   return fun1();
}

int main()
{
   fun1(); 
   fun2();
   return 0;
}

This above will be print 10, 10 according to static scoping, and 10, 7 in dynamic scoping.

Notes :

  • Local variables are the variables that are declared inside a block or a function. These variables can only be used inside that block or function.
  • Global variable is the variable that is declared outside of a block or function. These variables can be accessed from anywhere in the program.



Please write comments if you find anything incorrect. A gentle request to share this topic on your social media profile.

Comment
Next tutorial
CPP_Programmer Published 27 Aug 2020 · 2 min read

Related tutorials

  • Call by value, call by address and call by reference in C++

    The parameters of the function call are known as actual parameters and the parameters of the function definition are known as formal parameters. There are 3 ways of…

    3 min read
  • What should we use void main() or int main() ?

    There is question that what should we use void main() or int main() ? void main() { /* ... */ } Or, int main() { /* ... */…

    2 min read
  • C Comments

    Prerequisite – C Language Introduction C comments explain code and improve readability. These do not affect program execution. You can use comments in C to clarify code and describe…

    1 min read
  • C Programming Language Standard

    Prerequisite – C Language Introduction The C programming language has various versions: C89/C90, C99, C11, and C18. GATE CS 2027 Online Test Series72 tests in the real GATE…

    2 min read
  • Features of C Programming Language

    Prerequisite – C Language Introduction C is a simple language made in 1972 by Dennis Ritchie. It’s for system programming. C has low-level memory access, basic keywords, good…

    1 min read
  • C Language Introduction

    C is a programming language developed by Dennis Ritchie in 1972. It has a significant historical impact. It was originally created at Bell Laboratories of AT&T Labs for…

    4 min read