范围隐藏在C

  • 本文关键字:隐藏 范围 c scope
  • 更新时间 :
  • 英文 :


C是否具有范围隐藏?

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

int x = 3; 

我可以在函数内部"declare"还是在int x中main"other"?

是的,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
}

是的,但有些编译器会抱怨或被告知抱怨。对于gcc,请使用-Wshadow

是,C中存在范围隐藏。
局部作用域中的变量将在全局作用域中隐藏相同的命名变量。

是。这是非常有可能的。请浏览此帖子,详细解释C 中的各种范围

最新更新