Program with Scope Resolution

The scope of a variable is that part of program where variable can be used. It extends from starting of a block to the end of a block.

To access a global variable ::(scope resolution) is used.

This program shows three variables. One variable is global with value 20, other variable is in the main with value 26 and the third is in the block with value 32.

#include<iostream>
using namespace std;
int a = 20;
int main(){
	int a = 26;
	cout<<"Value of a is "<<a<<endl;
	{
		int a = 32;
		cout<<"Value of a is "<<a<<endl;
	}
	cout<<"Value of a is "<<a<<endl;
	cout<<"Value of a is "<<::a<<endl;	
	return 0;
}
program with scope resolution
program with scope resolution

One Reply to “Program with Scope Resolution”

Leave a Reply

Your email address will not be published. Required fields are marked *