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; } |
chevron_right
filter_none
Please write comments if you find anything incorrect in the above GFact or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Redeclaration of global variable in C
- Can Global Variables be dangerous ?
- How to print a variable name in C?
- Why variable name does not start with numbers in C ?
- Internal static variable vs. External static variable with Examples in C
- Hiding of all overloaded methods with same name in base class
- How Linkers Resolve Global Symbols Defined at Multiple Places?
- Initialization of global and static variables in C
- Why overriding both the global new operator and the class-specific operator is not ambiguous?
- Can we access private data members of a class without using a member or a friend function?
- Local Classes in C++
- How to return a local array from a C/C++ function?
- Local Labels in C
- How to use getline() in C++ when there are blank lines in input?
- Problem with scanf() when there is fgets()/gets()/scanf() after it
- Is there any need of "long" data type in C and C++?
- Is there any equivalent to typedef of C/C++ in Java ?
- Name Mangling and extern "C" in C++
- Comment in header file name?
- C program to store Student records as Structures and Sort them by Name

