2

Possible Duplicate:
How can I access a shadowed global variable in C?

In C++, I can use :: operator to specify a global variable. For example:

using namespace std;
int foo = 10;
int main(){
     int foo = 5;
     cout<<" Global variable: "<< ::foo <<endl;
     cout<<" Local Variable: " << foo <<endl;
     return 0;
}

How can I do this in C?

Community
  • 1
  • 1
André
  • 139
  • 1
  • 2
  • 10

1 Answers1

8

This was subject of an earlier question. It can be achieved as follows

int foo = 10;
int main(void) {
     int foo = 5;
     {
         extern int foo;
         foo++;
     }
     foo++
     return 0;
}

However in practice, I can't imagine running into this problem, because I could always rename the local variable or create a small static inline function that can access the global foo and that I could call.

Community
  • 1
  • 1
Johannes Schaub - litb
  • 496,577
  • 130
  • 894
  • 1,212