Scope Rules:
Local
variables & Global variables:-
            These are two types of variables.
1.           
Local Variables
2.           
Global Variables.
a)
Local Variables:-
ü  The
variables declared inside  a function are
known as local variables.  The values of
these variables cannot be accessed by any other function.
Eg:-       void
sum (int a, int b)
            {
                    int z;
                   z=a+b;
              
return (z);
            }
Here
z is a Local Variable which is declared in the function sum.  So, the value of z is restricted to the
function sum.
Eg
prog:-         void main()
{
        int
a=1,b=2;    /* local variables */
        printf(“in
main( ), a=%d,b=%d”,a,b);
   fun( );
}
void fun()
{       int
a=6,b=5;   /* local variables */
         printf(“in
fun( ),a=%d,b=%d”,a,b);
}
b)  Global Variables:-
            The variables declared outside the
main( ) function are known as global variables. 
These variables can be accessed by all the functions of the program.
Eg
prog:-         
int a=3,b=4;   /* Global variables */
void main( )
{
   printf(“in
main(), a=%d,b=%d”,a,b);
    a++;
     b++;
fun( );
}
void fun( )
{
         printf(“in
fun( ),a=%d,b=%d”,a,b);
}
No comments:
Post a Comment