Blocks and Scopes
beginner11 min readLesson 26 of 148
A name lives from its declaration to the end of its block — and inner names shadow outer ones.
Block scope
A block is { ... }. A name declared inside a block exists from its
declaration to that block's closing brace:
int main(void) {
int x = 1; // visible to the end of main
{
int y = 2; // visible inside these braces only
x = x + y;
}
// y does not exist here
return x; // 3
}
Loop and if bodies are blocks too — the classic bug:
for (int i = 0; i < 3; i++) { } // i exists only in the loop
// i++ here is an error
Shadowing
An inner declaration with the same name hides the outer one:
int total = 100;
{
int total = 5; // a DIFFERENT variable
printf("%d\n", total); // 5
}
printf("%d\n", total); // 100 — untouched
Shadowing is legal but confusing. Avoid reusing names in nested scopes.