Is there a scope resolution operator in C language?

munjal007 picture munjal007 · Feb 3, 2015 · Viewed 10.9k times · Source

I am reading a book on the C language ('Mastering C'), and found the topic on scope resolution operator (::) on page 203, on Google Books here.

But when I run the following code sample (copied from the book), the C compiler gives me an error. I searched on the internet but I am unable to find any reference to a scope resolution operator in C.

#include <stdio.h>
int a = 50;
int main(void)
{
    int a =10;  
    printf("%d",a);
    printf("%d\n", ::a);        
    return 0;
}

So if I want to access a global variable then how could I do that from within the main() function ?

Answer

abligh picture abligh · Feb 3, 2015

No. C does not have a scope resolution operator. C++ has one (::). Perhaps you are (or your book is) confusing C with C++.

You asked how you could access the global variable a from within a function (here main) which has its own local variable a. You can't do this in C. It is lexically out of scope. Of course you could take the address of the variable somewhere else and pass that in as a pointer, but that's a different thing entirely. Just rename the variable, i.e. 'don't do that'