C - Scope Rules


Scope Rules
 A region of the program where a defined variables can have its existence and beyond that variable it cannot be accessed is known as the Scope. In C programming there are 3 places where variables can be declared:-
·         Inside a block or a function which is called as local variables.
·         Outside of all functions which is known as global variables.
·         In the definition of function parameters which are called formal parameters.
Let us know more about local variables, global variables and formal parameters.
Local Variables

Variables that are declared inside a block or a function are known as ‘Local variables’. They can only be used by the statements that are inside that block or function of code. Local variables are not known to functions outside their own block. Here is an example of how a local variable is used. All the variables a, b, and c are local to 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 usually on the top of the program, outside a function. Global variables hold their values throughout the program and can be accessed inside any of the functions defined for the program.

Any function can access a global variable. A global variable is available for use throughout the program after its declaration. Below we have given an example to show how a 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 same name for global and local variables but the value of local variable 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;
}
When the above code is compiled and executed, it gives the following result −
value of g = 10


Formal Parameters
These are treated as local variables within a function. They take precedence over global variables. Following is an example:-
#include <stdio.h>

/* global variable declaration */
int a = 20;

int main () {

  /* local variable declaration in main function */
  int a = 10;
  int b = 20;
  int c = 0;

  printf ("value of a in main() = %d\n",  a);
  c = sum( a, b);
  printf ("value of c in main() = %d\n",  c);

  return 0;
}

/* function to add two integers */
int sum(int a, int b) {

   printf ("value of a in sum() = %d\n",  a);
   printf ("value of b in sum() = %d\n",  b);

   return a + b;
}
When the above code is compiled and executed, it gives the following result −
value of a in main() = 10
value of a in sum() = 10
value of b in sum() = 20
value of c in main() = 30
Initializing Local and Global Variables
When we define a local variable, we must initialize it our self as it is not initialized by the system. Global variable are automatically initialized by the system when we define them as follows:-
Data Type
Initial Default Value
int
0
char
'\0'
float
0
double
0
pointer
NULL
It is a good programming practice to initialize variables properly, otherwise our program may produce unexpected results, as uninitialized variables will take some garbage value already available at their memory location.




No comments:

Post a Comment