Difference between Local variable and Global variable – Programmerbay https://programmerbay.com A Tech Bay for Tech Savvy Tue, 06 Oct 2020 06:13:30 +0000 en-US hourly 1 https://wordpress.org/?v=6.5.5 https://programmerbay.com/wp-content/uploads/2019/09/cropped-without-transparent-32x32.jpg Difference between Local variable and Global variable – Programmerbay https://programmerbay.com 32 32 Difference between Local variable and Global variable in Tabular form https://programmerbay.com/difference-between-local-and-global-variable/ https://programmerbay.com/difference-between-local-and-global-variable/#respond Sun, 02 Feb 2020 07:19:23 +0000 https://www.programmerbay.com/?p=4427 What is a Variable?

A variable is a name of a storage location where data gets stored. Using that name one can perform operations and manipulations on the values.

  • A variable should not be named after a reserved keyword.
  • Unlike constants, a variable holding value can be changed at runtime.

Each and every variable has a lifetime to which extent it is visible within a particular block called scope, and outside of that block, the variable doesn’t have any existence.

These variables can be categorised in local, global and static.

In this article, we mainly focus on global and local variable.

local variable and global variable

Scope: A scope can be defined as an area to which a variable,method, class and interface is visible.

In C, if we compare local and global variable there are the following differences:

Difference between Local variable and Global variable in Tabular form

GLOBAL VARIABLELOCAL VARIABLE
A global variable can be accessed throughout a programA local variable can only be accessed within a function or block in which it is defined
A global variable, if not initialized, gets a default valueA local variable, if not initialized, holds an undefined value called garbage value
Within a function, by default, if both global and local variable has the same name, the compiler would never refer to a global variable over a local variableWithin a function, by default, if both global and local variable has the same name, the compiler always refer to a local variable instead of global
If there is any manipulation made it will visible to all the functionIf there is any manipulation made it will noticeable only within the associated function
It is is also known as an external variableIt can be termed as an automatic variable
It gets fixed location, meaning no matter in which function it is being usedIt may get a different location each time when calls to different functions are made
It is defined outside the main functionIt is defined within a function or a block
No need to declare in every functionIt is required to be declared each time for different function

Local Variable

A local variable is declared inside a function or a block and its scope is always limited to that particular function or block. It remains alive until that particular block or function completes its execution.

Local variables are local to a block or function and go out of scope at the time exiting from the block or curly braces ({}).

Global Variable

Global variables is a variable that is accessible to entire program and is available to all the functions or blocks. It can be accessible to all the functions in a program.

Therefore, two or more variables can have same name but they must be in different scope.

C Program to demonstrate the behaviour of Local variable and Global variable

#include <stdio.h>

// Global variable
int globalVariable = 10;
int main()
{
   // This variable can't be accessed in other functions
  int localVariable = 80;
  printf("I am Global Variable %d \n",globalVariable);
  printf("I am Local Variable %d ",localVariable);

    return 0;
}

Advantages

Local Variable :

  • It can have same name as global variable as its visibility always limit within a function or blocks
  • It gets destroyed immediately after that particular function completes its execution.
  • Since, scope of a local variable is restricted within a function or block, therefore, it can be accessed only to that function. As a result, this prevents it from getting unpredictable changes

Global Variable :

  • It can be accessed throughout the program
  • It allows multiple functions to work on the same data

Disadvantages

Local Variable:

  • Since, a local variable can have same name as global variable and other local variables defined in different scope, Therefore, it is quite complex to debug
  • It doesn’t allow data sharing
  • It cannot retain a value as static variable can
  • It restricts to a single block or function

Global Variable

  • It can contain unpredictable values as it provides access to multiple function
  • Using global variables in large quantity can lead to OutOfMemory error

]]>
https://programmerbay.com/difference-between-local-and-global-variable/feed/ 0