C是否隐藏了范围?
例如,如果我有一个全局变量:
int x = 3;
我可以在一个函数内部'声明'或另一个'另一个'int x?
答案 0 :(得分:5)
是的,这就是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
}
答案 1 :(得分:3)
是的,但有些编制者抱怨或可能被告知投诉。对于gcc
,请使用-Wshadow
。
答案 2 :(得分:1)
是范围隐藏存在于C.
局部范围中的变量将在全局范围内隐藏相同的命名变量。
答案 3 :(得分:0)
是。这是非常可能的。有关C