Scope in C

Scope Definition

Scope is defined as region or block in which a variable is declared, defined, and utilised; the variable is removed automatically when the region or block expires.

It can also be remembered as lifetime of a variable

Scope of variable in C

Representation of Scope :

int main() { --------1--------------------------------------------|
                                                                  |
    int a;           // scope of a                                |
                                                                  |
    func() {  -------2----------------|                           |
        int b;  //scope of b          |                           |
                                      |                           |
    } ---------------3----------------                            |
                       // scope of a                              |
    return 0;                                                     |
} ----------------4-----------------------------------------------|

In the above code , scope of variable a is from 1 to 4 space while scope of variable b is from 2 to 3 space.As variable a is declare in main() scope while variable b is declared in func() scope.

Types of variables

In the C programming language, variables can be defined in three different places.

  • Local variables are variables found within a function or a block.
  • Global variables are variables that exist outside of all functions.
  • Formal parameters are used in the defining of function parameters.

 Local Variables : 

Local variables are those that are defined within a function or block.Only statements included within that function or code block are permitted to use them. Outside of their own function, local variables are unknown.

Below example are representing the scope of local variables a and b.

#include<stdio.h>

int main() { -------------------------------1-----------------|
                                                              |
    int a, b;   // local variable                             
                                                              |
    a = 20;                                                   |
    b= 10;                                                    |
                                                              |
    return 0;                                                 |
}-------------------------------------------2-----------------|

Global variable :

Outside of a function, typically on top of the program, global variables are defined. Global variables can be accessed inside any of the functions created for the program and keep their values during its execution. Any function may access a global variable.
In other words, once a global variable has been declared, it can be used throughout the entire program. 

Below example are representing the scope of global variables c and d.

#include <stdio.h> ----------------------------------------1--|
int c,d; // global variable
int main() { 

int a,b; //local variable 
                                                              |
c=20;                                                         |
d=10;                                                         |
                                                              | 
return 0;                                                     |
} -------------------------------------------------------2----|

Formal Variable :

Formal parameters are prioritised over global variables and are handled as local variables within a function.

Below example are representing the functioning of formal variables.

Run
#include<stdio.h>
 
int a = 30; // global variables
 
int main () {

  
  int a = 10;  //local variables
  int b = 15;
  int c = 0;

  printf ("value of a in main() = %d\n",  a);
  c = product( a, b);
  printf ("value of c in main() = %d\n",  c);

  return 0;
}

/* function  */
int product(int a, int b) {

   printf ("value of a in product() = %d\n",  a);
   printf ("value of b in product() = %d\n",  b);

   return a * b;
}

Output :

value of a in main() = 10
value of a in product() = 10
value of b in product() = 15
value of c in main() = 150

For more related content : Click-Here