Tuesday 31 December 2013

Filled Under:

Scope of a variable

Scope of a variable:

A variable can has global scope or local scope.

Local scope:

         If we declare a variable within main function. Then its scope will be local. We can only use these variable only main function if we use it other  than main function then compiler will generate the error .So we can say that local variable are those variable which we declare within body of function  or block. And its will be within the block or function.

Example:

     Main()
{
Int a;
Char c, b ;
}
Here a,b and c are local variable.

Global variable:

                           Global variable are those variable which we will declare outside the function.  And its scope will be over the entire program. We can use it anywhere in the program or any function or Global variables can be referred from anywhere in the code, even inside functions, whenever it is after its declaration.

Example:

Int a;
Char b,c;
Main()
{
…………
………………..
………………….
}
Here a,b and c are the global variable.

Note:

         If we use same variable name as different data type then compiler will generate the error

  Example:

            Int a;
           Char a;







0 comments:

Post a Comment