如何在C中指定范围?

Sta*_*aro 2 c variables scope

是否可以在C中指定范围?

int x = 5;

void setX(int x) {
    (this, self, etc).x = x;
}
Run Code Online (Sandbox Code Playgroud)

我知道不能有一个thisself在C中,因为它不是一个对象.有没有办法做到这一点?

how*_*rdh 5

您可以使用extern这样访问全局变量:

int a = 10;

int foo(int a)
{
    int b;
    {
        extern int a;
        b = a;
    }
    //b now contains the value 10
}
Run Code Online (Sandbox Code Playgroud)