Local and Global Variable
There are two types of variables. These are Local Variable and Global Variable.
Local Variable: The variables which are defined within a body of the function or block are local to that function or block only and only called local variable. For Example-
func (i,j)
int i,j;
{
int a,b; //Local Variable
.............
}
Here a and b are the local variables which are defined within the body of the function func( ). Local variable can be used for only that function, in which they are defined. The same variable name may be used in different functions and these variables are local to that function only. For example,
var1(i, j)
int i, j;
{
int a,b;
a=5;
b=5;
.............
.............
}
var2(m,n)
int m,n;
{
int a,b;
a=15;
b=20;
.............
.............
}
Here value of a=5 and b=5 is local to the function var1 () and a=15, b=20 is local to the function var2 ().
Global Variable: The variables which are defined outside the main() function is called global variable. The global variable has the same data type and same name throughout the program. It is useful to declare the variable global when the variable has constant value throughout the program. For Example-
int a,b=6;
main()
{
a=8;
...............
...............
func();
}
func()
{
int mult;
mult=a*b;
...............
...............
}
Here a and b are the global variable.