#include <iostream>
usingnamespace std;
void t1(); // function prototype
void t2(); // function prototype
int y = 2; // global variable
int main()
{
t1();
t2();
system ("Pause");
return 0;
}
void t1()
{
int x = 1; // local variable
int y=8;
cout <<"in t1 x is "<< x << endl;
cout <<"in t1 y is "<< ::y << endl;
x++;
y++;
}
void t2()
{
int x = 1; // local variable
int y=8;
cout <<"in t2 x is "<< x << endl;
cout <<"in t2 y is "<< y << endl;
}
Output
1 2 3 4 5
in t1 x is 1
in t1 y is 2
in t2 x is 1
in t2 y is 8
:: is a scoping operator. ::y is the global y variable defined at line 8. If you omitted it then the code would print the value of the local variable y defined at line 23.
int y = 2; //global variable
int main() {
int y = 5; local variable
cout << ::y; /* it will call y in global scope which is 2 instead of y in local scope*/
}