Can we access global variable if there is a local variable with same name?
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.
Recommended Posts:
- Redeclaration of global variable in C
- How to print a variable name in C?
- Why variable name does not start with numbers in C ?
- Variable Length Argument in C
- How to modify a const variable in C?
- Using a variable as format specifier in C
- Different ways to initialize a variable in C/C++
- Variable Length Arrays in C and C++
- C | Variable Declaration and Scope | Question 6
- C | Variable Declaration and Scope | Question 3
- C | Variable Declaration and Scope | Question 2
- C | Variable Declaration and Scope | Question 1
- C | Variable Declaration and Scope | Question 4
- C | Variable Declaration and Scope | Question 7
- C | Variable Declaration and Scope | Question 8



