Scope hiding in C

a 夏天 提交于 2019-12-10 09:10:50

问题


Does C have scope hiding?

For example, if I have a global variable:

int x = 3; 

can I 'declare' inside a function or main 'another' int x?


回答1:


Yes, that's how C works. For example:

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
}



回答2:


Yes but some compilers complain or can be told to complain. For gcc, use -Wshadow.




回答3:


Yes Scope Hiding exists in C.
A variable in local scope will hide the same named variable in global scope.




回答4:


Yes. This is very much possible. Please go through this post for a detailed explanation on various scope in C



来源:https://stackoverflow.com/questions/8928521/scope-hiding-in-c

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!