Local and 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().
Explanation:
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(" = %d\n" , i);
b = value(i)
printf(" j = %d\n");
} /* 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 b is also local to the main() but has no scope in the function value().
That's All
That's All
0 Response to "Explanation of Local and Global Variables with Example"
Post a Comment