Skip to content
Related Articles

Related Articles

Can we access global variable if there is a local variable with same name?
  • Difficulty Level : Easy
  • Last Updated : 22 Jan, 2021

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 (::)


// C++ Implementation
#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;
    return 0;
Value of global x is 0
Value of local x is 10

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

Want to learn from the best curated videos and practice problems, check out the C++ Foundation Course for Basic to Advanced C++ and C++ STL Course for foundation plus STL.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.
My Personal Notes arrow_drop_up
Recommended Articles
Page :