范围隐藏在C中

时间:2012-01-19 15:18:33

标签: c scope

C是否隐藏了范围?

例如,如果我有一个全局变量:

int x = 3; 

我可以在一个函数内部'声明'或另一个'另一个'int x?

4 个答案:

答案 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

中各种范围的详细说明,请浏览this帖子