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?
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
}
Yes but some compilers complain or can be told to complain. For gcc
, use -Wshadow
.
Yes Scope Hiding exists in C.
A variable in local scope will hide the same named variable in global scope.
Yes. This is very much possible. Please go through this post for a detailed explanation on various scope in C