Can we access global variable if there is a local variable with same name?

1.3

In C, we cannot access a global variable if we have a local variable with same name, but it is possible in C++ using scope resolution operator (::).

#include<iostream>

using namespace std;

int x;  // Global x

int main()
{
  int x = 10; // Local x
  cout<<"Value of global x is "<<::x<<endl;
  cout<<"Value of local x is "<<x;  
  getchar();
  return 0;
}

Please write comments if you find anything incorrect in the above GFact or you want to share more information about the topic discussed above.

GATE CS Corner    Company Wise Coding Practice

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.

Recommended Posts:



1.3 Average Difficulty : 1.3/5.0
Based on 20 vote(s)










Writing code in comment? Please use ide.geeksforgeeks.org, generate link and share the link here.