local and global variables in C Language

 

What is a Global Variable?

  • Global variables are those variables which are declared outside of all the functions or block and can be accessed globally in a program.
  • It can be accessed by any function present in the program.
  • Once we declare a global variable, its value can be varied as used with different functions.
  • The lifetime of the global variable exists till the program executes. These variables are stored in fixed memory locations given by the compiler and do not automatically clean up.
  • Global variables are mostly used in programming and useful for cases where all the functions need to access the same data.

Example:

  1. #include<stdio.h>  
  2. int a=50, b=40;  
  3. void main()  
  4. {  
  5.   printf("a = %d and b=%d",a,b);  
  6. }  

In the above example, a and b are the global variables.

Advantages of Global Variable

  • Global variables can be accessed by all the functions present in the program.
  • Only a single declaration is required.
  • Very useful if all the functions are accessing the same data.

Disadvantages of Global Variable

  • The value of a global variable can be changed accidently as it can be used by any function in the program.
  • If we use a large number of global variables, then there is a high chance of error generation in the program.

What is a Local Variable?

  • Variables that are declared within or inside a function block are known as Local variables.
  • These variables can only be accessed within the function in which they are declared.
  • The lifetime of the local variable is within its function only, which means the variable exists till the function executes. Once function execution is completed, local variables are destroyed and no longer exist outside the function.
  • 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 "static" keyword, we can retain the value of local variable.

Example:

  1. #include<stdio.h>  
  2. void main()  
  3. {  
  4.    int x=50, y=40;  
  5.    printf("x = %d and y=%d",x, y);  
  6. }  

In the above example, we have declared x and y two variables inside the main function. Hence these are local variables.

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 only for the limited time when the function is executed; after that same memory location can be reused.

Disadvantages of Local Variables

  • The scope of the local variable is limited to its function only and cannot be used by other functions.
  • Data sharing by the local variable is not allowed.
Comparison Chart Between Global Variable and 
Local Variable



Global VariableLocal Variable
Global variables are declared outside all the function blocks.Local Variables are declared within a function block.
The scope remains throughout the program.The scope is limited and remains within the function only in which they are declared.
Any change in global variable affects the whole program, wherever it is being used.Any change in the local variable does not affect other functions of the program.
A global variable exists in the program for the entire time the program is executed.A local variable is created when the function is executed, and once the execution is finished, the variable is destroyed.
It can be accessed throughout the program by all the functions present in the program.It can only be accessed by the function statements in which it is declared and not by the other functions.
If the global variable is not initialized, it takes zero by default.If the local variable is not initialized, it takes the garbage value by default.
Global variables are stored in the data segment of memory.Local variables are stored in a stack in memory.
We cannot declare many variables with the same name.We can declare various variables with the same name but in other functions.

Comments

Popular posts from this blog

PHP Array Functions

IMPLEMENTATION OF LRU PAGE REPLACEMENT ALGORITHM

Tableau(Line Graphs)