I'm having difficulty understanding this concept.
Consider the following snippet in C:
Code:
int main(int argc, char* argv[]) {
int x=5;
{ // nested block
printf("%d\n", x);
}
printf("%d\n", x);
return 0;
}
Output:
5
5
This snippet implies that x is defined inside the nested block.
So, if it is defined inside the nested block, and we all know this is illegal:
Code:
int main(int argc, char* argv[]) {
int x=5;
int x=6;
printf("%d\n", x);
return 0;
}
then how is it that the following is legal?
Code:
int main(int argc, char* argv[]) {
int x=5;
{ // nested block
int x=6;
printf("%d\n", x);
}
printf("%d\n", x);
return 0;
}
Output:
6
5