Scope of the variable is the life time of the variable in the program.
A variable can either global or local scope.
Global Variable is a declared in the main body of the source code outside all functions while the local variable is the one declared within the body of function or block.
In 'C' the global version version of the variable of the variable can not be accessed from within the inner block.
In C++ , scope resolution operator (::) is used to access global variable.
Syntax: ::global_variable_name;
#include<conio.h>
#include<iostream.h>
int a = 10;
void main( )
{
int a = 15;
clrscr( );
cout<<"\n\n\t Local a is -->"<<a<<"\tGlobal a is -->"<<::a;
a = 20;
cout<<"\n\n\t Local a is -->"<<a<<"\tGlobal a is -->"<<::a;
getch( );
}
OutPut:-
Local a is -->15 Global a is -->10
Local a is -->20 Global a is -->10
A variable can either global or local scope.
Global Variable is a declared in the main body of the source code outside all functions while the local variable is the one declared within the body of function or block.
In 'C' the global version version of the variable of the variable can not be accessed from within the inner block.
In C++ , scope resolution operator (::) is used to access global variable.
Syntax: ::global_variable_name;
#include<conio.h>
#include<iostream.h>
int a = 10;
void main( )
{
int a = 15;
clrscr( );
cout<<"\n\n\t Local a is -->"<<a<<"\tGlobal a is -->"<<::a;
a = 20;
cout<<"\n\n\t Local a is -->"<<a<<"\tGlobal a is -->"<<::a;
getch( );
}
OutPut:-
Local a is -->15 Global a is -->10
Local a is -->20 Global a is -->10
0 Comments:
Post a Comment