Scope of variable:
The scope of variable defines the variable access point and life of variable. There are two variable scopes:- Local variable
- Global variable
Local variable:
A type of variable that is declared inside a function is called as global variable.
The life of a local variable ends when a function ends and can only be accessed within the function in which it is declared.
Global variable:
A type of variable that is declared outside of any function is called as a global variable. It is generally declared after preprocessor directives and before main () function.
The life of a global variable ends when the execution of programs ends and it can be accessed by any function of the program.
Example:
#include<iostream.h>
#include<conio.h>
int a;
void main()
{
int b=10;
a=110;
cout<< “value of a: ”<<a ;
cout<<”\n value of b:”<<b;
getch();
}
In the above example "int a" is a globle variable which means that it can be accessed through out the program and "int b" is a local variable which means that it can only be accessed within the main(), not outside the main ().
No comments:
Post a Comment