Local and Global Variable in C

Here are Local and Global Variable in C

The local and Global variables in C are the Scope Variable. The scope of variables tends to the area of the program. 

The difference between Local and Global variables is that local variables can be accessed only within the function or block in which they are defined and global variables can be accessed globally in the entire program where the variables can be accessed after its declaration.

 

Local and Global Variable in C

 

Local variable

Variables that are declared inside a function or block are  local variables

    • The reason for the limited scope of local variables is that local variables are stored in the stack, which is dynamic in nature and automatically cleans up the data stored within it.
    • But by making the variable static with the “static” keyword, we can retain the value of a local variable.

Program

#include <stdio.h>
void function() {
  int x = 10;    // local variable
}
 
int main()
{
  function();
}

 

Advantages of Local Variable

    • The same name of a local variable can be used in different functions as it is only recognized by the function in which it is declared.
    • Local variables use memory for the limited time when the function is executed, and the same memory location can use again.

 

Disadvantages of Local Variables

    • The scope of the local variable is limited to its function and cannot be used by other functions.
    • A local variable is not allowed to data sharing

 

 

Global variable

A variable that is declared outside the function or block is a global variable. 

    • When we declare a global variable, its value can be changed as used with different functions.
    • The lifetime of the global variable is till the program executes.
    • Global variables are useful for cases where all the functions need to access the same data.

Program

#include <stdio.h>
int x = 30;                    //global variable
void function1()
{
  printf("%d\n" , x);
}
void function2()
{
  printf("%d\n" , x);
}
int main() {
 
  function1();
  function2();
    return 0;
}

Output

30
30

 

Advantages of Global Variable

    • Global variables can be retrieved by all the functions present in the program.
    •  Required only a single declaration.
    •  All the functions are accessing the same data then the global variable is very useful.

 

Disadvantages of Global Variable

    • The value of a global variable can change accidentally as it can be used by any function in the program.
    • There is a high chance of error generation in the program when we use a large number of global variables.

 

 

Difference Between Local and Global variable

Parameter Local Global
Scope It is declared inside a function. It is declared outside the function.
Value If it is not initialized, a garbage value is a store If it is not initialized zero is stored as default.
Lifetime It is created when the function starts execution and lost when the functions terminate. It is created before the program’s global execution starts and lost when the program terminates.
Data sharing It is not possible as data of the local variable can be accessed by only one function. Data sharing is possible as multiple functions can access the same global variable.
Parameters Parameters passing is required for local variables to access the value in another function It passing is not necessary for a global variable as it is visible throughout the program
Modification of variable value When the value of the local variable is modified in one function, the changes are not visible in another function. When the value of the global variable is modified in one function changes are visible in the rest of the program.
Accessed by Local variables can be accessed with the help of statements, inside a function in which they are declared. You can access global variables by any statement in the program.
Memory storage It is stored on the stack unless specified. It is stored in a fixed location decided by the compiler.

 

Examples to understand differences between Local and Global Variable

Program

#include<stdio.h>  
  
    // Global variables  
    int a;  
    int b;  
    int Add()  
    {  
  
        return a + b;  
    }  
  
    int Mul()  
  
    {  
    int c=10; //Local Variable  
    int d=20;  ////Local Variable  
    return c*d;  
    }  
    void main()  
    {  
        int Ans1, Ans2, c=30;// Local variable  
        a = 50;  
        b = 70;  
          
        Ans1 = Add();  
        Ans2= Mul();  
          
        printf("The addition result is: %d\n",Ans1);  
                  printf("The Multiplication result is: %d\n",Ans2);  
                  printf("%d\n", c);  
    }

Output

The addition result is: 120
The Multiplication result is: 200
30

 

 

Also, read Function Pointer in C

Share this post

Leave a Reply

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