What is the scope of a Variable? Explain the difference between a global and local variables with example programs.

 What is the scope of a Variable? Explain the difference between global and local variables with example programs.

A scope in any programming is a region of the program where a defined variable can have its existence and beyond that variable, it cannot be accessed. There are three places where variables can be declared in C programming language −

  • Inside a function or a block which is called local variables.

  • Outside of all functions which are called global variables.

  • In the definition of function parameters which are called formal parameters.

Let us understand what are local and global variables, and formal parameters.

Local Variables

Variables that are declared inside a function or block are called local variables. They can be used only by statements that are inside that function or block of code. Local variables are not known to functions outside their own. The following example shows how local variables are used. Here all the variables a, b, and c are local to the main() function.

#include <stdio.h>
 
int main () {

  /* local variable declaration */
  int a, b;
  int c;
 
  /* actual initialization */
  a = 10;
  b = 20;
  c = a + b;
 
  printf ("value of a = %d, b = %d and c = %d\n", a, b, c);
 
  return 0;
}

Global Variables

Global variables are defined outside a function, usually on top of the program. Global variables hold their values throughout the lifetime of your program and they can be accessed inside any of the functions defined for the program.

A global variable can be accessed by any function. That is, a global variable is available for use throughout your entire program after its declaration. The following program show how global variables are used in a program.

#include <stdio.h>
 
/* global variable declaration */
int g;
 
int main () {

  /* local variable declaration */
  int a, b;
 
  /* actual initialization */
  a = 10;
  b = 20;
  g = a + b;
 
  printf ("value of a = %d, b = %d and g = %d\n", a, b, g);
 
  return 0;
}

A program can have the same name for local and global variables but the value of the local variables inside a function will take preference. Here is an example

#include <stdio.h>
 
/* global variable declaration */
int g = 20;
 
int main () {

  /* local variable declaration */
  int g = 10;
 
  printf ("value of g = %d\n",  g);
 
  return 0;
}

Differences between global and local variables

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 stored

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

Data sharing 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 other function

Parameters 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 on a fixed location decided by the compiler.


What do you think?
I hope, now you have a better understanding of Variables.
 Comments and suggestions regarding this article are welcome.

Comments

Popular posts from this blog

What is a string ? Write a function in C for string concatenation. Without the use of inbuilt string function?

What is a pointer ? Write a C program using pointer to print the name and price of the items sold in a retail shop on a specific date.