Local and Global variables

Local variables: 

Variable whose existence is known only to the main program or functions are called local variables. Local variables are declared with in the main program or a function.
 
Global variables: 
Variables whose existence is known to the both main() as well as other functions are called global variables. Global variables are declared outside the main() and other functions.
The following Program illustrates the concept of both local as well as global variables.

Statement of C Program: This program does not accept anything from the keyboard. It initialises the variables a to 10 outside the main() function and value().
 
#include<stdio.h>
int a = 10;                                 /* global declaration */
main()
{
int b;
printf("  = %dn" , i);
b = value(i)
printf(" j = %dn");
}                                                              /* End of main() */
                    /* Function to compute value */
int value(i)
int i;
{
int c;
c = i + 10;
return(c);
}                                           /* End of Function */
 

Output:
 i = 10
 b = 20

Explanation:
The statement int  i = 10;   that appears before the main() is a global declaration. The value of i is accessed by the main program as well as the function value(). The variable c is local to the function value(). It has no existence in the function value(). The variable is also local to the main() but has no scope in the function value().
Posted in PHP | Comments Off

Comments are closed.