Is there a scope resolution operator in C language

2019-02-14 14:49发布

问题:

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 ?

回答1:

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'



回答2:

No, namespaces are a feature of C++.

It is, however, possible to refer to global a in your example. You can achieve this by using the extern keyword:

#include <stdio.h>

int a = 50;

int main(void)
{
    int a = 10;
    printf("%d\n",a);

    { // Note the scope
        extern int a; // Uses the global now
        printf("%d\n", a);
    }

    return 0;
}

That's a bit tricky, though. It's bad style. Don't do that.



回答3:

:: operator is available in C++ not C. If you wanted to access the global variable, use

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

Or you could use a pointer which holds the address of the global variable a and then dereference the pointer if you want to print the value of the global variable a.



回答4:

No (the :: operator is C++ specific). In C, if you use the same identifier in different overlapping scopes (say, file scope a and block scope a), the block scope identifier shadows the file scope identifier and there is no way to refer to the shadowed identifier.

It is generally best programming practice to avoid shadowed variables. Many lint type programs can warn about this situation.



回答5:

In plain C, there is no scope resolution. You have to name your variables differently.

That means that all variables a below are different ones:

#include <stdio.h>
int a = 50;
int main(void)
{
    int a = 10;
    {
        int a = 20;
        int i;
        for (i = 0; i < 10; i++) {
            int a = 30;
        }
    }
    return 0;
}


回答6:

You may use pointers to access and edit global variables in C.

#include <stdio.h>
#include <stdlib.h>

int a;
a=78;
int *ptr=&a;            //pointer for global a
int main()
{
    int a=0;
    printf("%d",a);         //Prints 0 as local variable
    printf("%d",*ptr);
    ptr=30;      //changes the value of global variable through pointer
    printf("%d",*ptr);      //Now it prints 30
    return 0;
}