Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a scope resolution operator in C language?

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 ?

like image 475
munjal007 Avatar asked Feb 03 '15 13:02

munjal007


1 Answers

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'

like image 187
abligh Avatar answered Sep 20 '22 15:09

abligh