Scope of Variables in C – Local,Global & Formal parameters

Scope of Variables in C define the visibility of a variable within the block or within function or a program. Unlike other programming languages C allows us to declare variables anywhere in the program but should be in their scope or visibility and should be declared before its use.

In simple, a scope of a variable can be understood as a region in a program where a declared variable can be accessed, modified and used for the purpose, beyond that region variable cannot be accessed. There are three different ways to declare variables in C language.

Consider below example program to find the Multiplication of two numbers.

 /* Scope of Variables in C - Example 1  */ 
#include <stdio.h>
int v1 = 10;   //Global variable
int multiply(int,int);
int main(){
int v2,v3;
int mul; // v2, v3 n mul are local variable

v2 = 25;
mul = v1*v2;
printf("Multiplication of v1 n v2 is: %d\n", mul);
v1 = 5;
v3 = 12;
mul = multiply(v1,v3);
printf("Multiplication of v1 n v3 is: %d\n", mul);

return 0;
}

int multiply(int a,int b)   // Formal parameters
{
int result;
result = a * b;

return result;
}

Output:

Multiplication of v1 n v2 is: 250
Multiplication of v1 n v3 is: 60

As you can see in the above example there are two functions main() to start the program and multiply() to perform multiplication. Apart from function the variables v1 is declared which is outside any function, so it can be accessible, modified in both functions and also outside the functions, you can see v1 modified in main function. Variable v1’s visibility is throughout the program so it is called as Global variable.

Next is local variables v2, v3 which are mostly declared at the beginning of the main or any other function. These local variables v2, v3 can only be accessed throughout the main function and cannot be accessed by other function or block in the program. This is why variables v2, v3 are called local variable whose scope is limited to the function or block in which it is being declared.

Third one is formal parameters which are similar to local variables which are only available inside the function.

Let’s study C scope rules in details using example.

Scope of Variables in C

Local Variables

Variables declared inside a region/function/block are called local variables. These variables also called as auto variables that can be utilized only by statements and expression inside that function or block of statements.

Following example shows how local variables declared, defined and used.

 /* Scope of Variables in C - Example 2  */ 
#include <stdio.h>
 
int main () {

  int var1, var2, sum; // Local
  
 
  var1 = 15;
  var2 = 28;
  sum = var1 + var2;
 
  printf ("Sum of var1 + var2 = %d\n",sum);
 
  return 0;
}

Output:

Sum of var1 + var2 = 43

Local Variables has limited lifetime can be used only inside a block or a function where the variables are declared. Outside that block or function it is undeclared variable and will be destroyed upon exiting the block.

Let’s see an another example using block inside a block and using variable outside its availability.

 /* Scope of Variables in C - Example 3  */ 

int main()
{

int var1;

var1 = 10;
printf("Outer Local variable var1 : %d\n", var1);

 { //Block B
 int var1;   // this var1 has higher preference than outer var1
 var1 = 50
 printf("Inner local variable var1 : %d\n", var1);
                }

printf("Outer var1 : %d\n", var1);  // this still the same

return 0;

 
}

Output:

Outer Local variable var1 : 10
Inner local variable var1 : 50
Outer var1 : 10

In the above example, value of variable var1 is 10 outside the block B and inside block B we have declared var1 again and assigned a value 50 to it, so the value of var1 is now 50. When we exit this block, var1 which holds 50 get destroyed and now the value of var1 become 10 which was outside the block.

Global Variables

Variables declared outside the function and accessed by any function are called Global variables. The availability of Global variables throughout and holds the value till lifetime of the program and available for use over entire program after its declaration. Global variables are accessible from outside ad inside the functions or blocks. In simple, we can access global variable from anywhere in the program.

The following example program show how global variables are used in a program.

 /* Scope of Variables in C - Example 3  */ 
#include <stdio.h>

int GVar1;
int GVar2 = 10; // global variable
int multiply();

int main () {
int sum, mul;  // local variable
printf("Value of GVar1 = %d and Gvar2 = %d \n", GVar1,GVar2);

GVar1 = 20;
sum = GVar1 + GVar2;
printf("Sum of GVar1 + GVar2 = %d + %d = %d\n\n", GVar1, GVar2, sum);

mul = multiply();
printf("*********Global Variable after modification*********\n\n");

printf("Values of GVar1 = %d and GVar2 = %d\n", GVar1, GVar2);

printf("Multiplication of GVar1 & GVar2 = %d X %d = %d\n", GVar1, GVar2, mul);
  return 0;
}


int multiply(){
GVar1 = 5;
GVar2 = 6;
return GVar1*GVar2;
}

Output:

Value of GVar1 = 0 and Gvar2 = 10
Sum of GVar1 + GVar2 = 20 + 10 = 30

*********Global Variable after modification*********

Values of GVar1 = 5 and GVar2 = 6
Multiplication of GVar1 & GVar2 = 5 X 6 = 30

As you can see in above example there are 2 global variables GVar1, GVar2, a local variable sum, mul and a function to multiply modified variables. The variable GVar2 assigned a value ‘10’ while Gvar1 got default value which is ‘0’ assigned by compiler. As we already know that global variable can be modified at any place inside or outside function in a program, so Gvar1 is reassigned value 20 inside main function which replace default value. And the sum of this two is 30, can be seen at output.

In the second part, we have modified variables GVar1 and GVar2 in function multiply (). Now variables hold the value 5 and 6 instead 10 & 20 which were assigned before main function.

Important: A program can have variable named similar for local and global variables but the value of local variable inside a function will have higher priority over global.

 /* Scope of Variables in C - Example 4  */ 

int GV = 50;

void printGV();
 
int main () {

  int GV = 13;
 
  printf ("Value of GV = %d\n",GV);
printGV();
 
  return 0;
}

void printGV(){

printf("printing GV = %d\n",GV);
}

Output:

value of GV = 13
printing GV = 50

So, the value inside the local block or functions will be holds that value until that block or functions closed. Local variables will be created that hides the global variable when execution enter the block and destroyed at exit from the block or functions. So, the function printGV() function got value of global variable.

Formal Parameters

Formal parameters are similar to local variables with-in a function and they take higher precedence over global variables.

 /* Scope of Variables in C - Example 5  */ 
#include <stdio.h>

/* global variable declaration */
int var1 = 50;

int sum(int,int);
int multi(int,int);

int main () {

  /* local variable declaration in main function */
  int var1 = 10;
  int var2 = 20;
  int Add = 0;
  int mul = 0;

  printf ("value of var1 inside main function = %d\n",var1);

  Add = sum(var1,var2);

  printf ("Sum = %d\n",Add);

  mul = multi(var1,var2);

  printf ("Multiply = %d\n",mul);
  return 0;
}

int sum(int var1, int var2) { // this are local variables

printf ("values in sum function are var1= %d, var2 = %d\n",var1,var2);


   return var1 + var2;
}

int multi(int a,int b){

printf ("values of var1 inside multi function are var1= %d\n",var1);

return a*b;
}

Output:

value of var1 inside main function = 10
values in sum function are var1= 10, var2 = 20
Sum = 30
values of var1 inside multi function are var1= 50
Multiply = 200 

Here, variable var1 and var2 are passed as formal parameters to the function sum() from main() which holds values 10 and 20. When this parameters are received with same name as that of global variable they have higher priority over global.

Now look at second function multi() whose receiving parameters are different from global variable so here var1 hold global value.

Default values of Local and Global Variables

When a local variable is defined, we must initialize it else they hold garbage value. Global variables are initialized automatically by the system. And it is always recommended to initialize variable to avoid unexpected results.

Data TypeDefault Values
int0
char‘\0’
float0
double0
pointerNULL

Don’t forget to Share and Subscribe!

References:

6 Comments

  1. hey there and thank you for your info – I have definitely picked up anything new from right here.

    I did however expertise a few technical points using this web site,
    since I experienced to reload the website a lot of times previous
    to I could get it to load correctly. I had been wondering if your web hosting is OK?
    Not that I am complaining, but slow loading instances
    times will sometimes affect your placement in google and can damage your high-quality score if advertising and marketing with Adwords.
    Well I am adding this RSS to my e-mail and could look out for a lot more of your respective intriguing content.
    Ensure that you update this again very soon.

  2. Hey there! Do you use Twitter? I’d like to follow you if that would be ok.
    I’m absolutely enjoying your blog and look forward to new updates.

  3. A person essentially lend a hand to make seriously posts
    I might state. This is the very first time I frequented your web
    page and thus far? I amazed with the research you made to create this actual
    post extraordinary. Magnificent process!

Leave a Reply

Your email address will not be published. Required fields are marked *

%d bloggers like this: