【发布时间】:2012-01-19 15:18:33
【问题描述】:
C 有范围隐藏吗?
例如,如果我有一个全局变量:
int x = 3;
我可以在函数或主“另一个”int x 中“声明”吗?
【问题讨论】:
C 有范围隐藏吗?
例如,如果我有一个全局变量:
int x = 3;
我可以在函数或主“另一个”int x 中“声明”吗?
【问题讨论】:
是的,这就是 C 的工作方式。例如:
int x;
void my_function(int x){ // this is another x, not the same one
}
void my_function2(){
int x; //this is also another x
{
int x; // this is yet another x
}
}
int main(){
char x[5]; // another x, with a different type
}
【讨论】:
x 声明为int,然后通过说您有一个char[5] 类型的新x,您将只能看到后者char x[5]。
是的,但有些编译器会抱怨或被告知抱怨。对于gcc,请使用-Wshadow。
【讨论】:
是的,C 中存在范围隐藏。
局部范围内的变量将在全局范围内隐藏相同的命名变量。
【讨论】:
是的。这是很有可能的。关于C中的各种作用域的详细解释请通过this帖子
【讨论】: