Is there a scope resolution operator in C language

2019-02-14 14:56发布

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 ?

6条回答
Summer. ? 凉城
2楼-- · 2019-02-14 15:13

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'

查看更多
爷的心禁止访问
3楼-- · 2019-02-14 15:13

:: 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楼-- · 2019-02-14 15:16

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.

查看更多
小情绪 Triste *
5楼-- · 2019-02-14 15:29

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;
}
查看更多
我欲成王,谁敢阻挡
6楼-- · 2019-02-14 15:33

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;
}
查看更多
我只想做你的唯一
7楼-- · 2019-02-14 15:34

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.

查看更多
登录 后发表回答