Which i does printf use?
Suppose a file declares `unsigned i = 1;`, then `main` declares another `unsigned i = 2;`. Inside an `if` block, `extern unsigned i;` refers to the file-scope object, so `printf` prints `1`. The local definition creates a distinct object; the inner declaration does not create a third one. section 13.2
Listing 13.2. An example of shadowing with an extern variable
1 #include <stdio.h>
2
3 unsigned i = 1;
4
5 int main(void) {
6 unsigned i = 2; /* A new object */
7 if (i) {
8 extern unsigned i; /* An existing object */
9 printf("%u\n", i);
10 } else {
11 printf("%u\n", i);
12 }
13 }
!@%STYLE%@!
{"css":"{\"css\": \"font-weight: bold;\"}","target":"[[{\"line\":0,\"ch\":1},{\"line\":0,\"ch\":2}],[{\"line\":2,\"ch\":18},{\"line\":2,\"ch\":19}],[{\"line\":9,\"ch\":0},{\"line\":9,\"ch\":1}],[{\"line\":10,\"ch\":0},{\"line\":10,\"ch\":1}],[{\"line\":11,\"ch\":0},{\"line\":11,\"ch\":1}],[{\"line\":12,\"ch\":0},{\"line\":12,\"ch\":1}],[{\"line\":0,\"ch\":5},{\"line\":0,\"ch\":13}],[{\"line\":1,\"ch\":1},{\"line\":1,\"ch\":2}],[{\"line\":5,\"ch\":20},{\"line\":5,\"ch\":21}],[{\"line\":11,\"ch\":1},{\"line\":11,\"ch\":2}],[{\"line\":2,\"ch\":1},{\"line\":2,\"ch\":2}],[{\"line\":12,\"ch\":1},{\"line\":12,\"ch\":2}],[{\"line\":2,\"ch\":5},{\"line\":2,\"ch\":13}],[{\"line\":5,\"ch\":7},{\"line\":5,\"ch\":15}],[{\"line\":7,\"ch\":16},{\"line\":7,\"ch\":24}],[{\"line\":3,\"ch\":1},{\"line\":3,\"ch\":2}],[{\"line\":4,\"ch\":1},{\"line\":4,\"ch\":2}],[{\"line\":4,\"ch\":5},{\"line\":4,\"ch\":8}],[{\"line\":8,\"ch\":11},{\"line\":8,\"ch\":14}],[{\"line\":10,\"ch\":11},{\"line\":10,\"ch\":14}],[{\"line\":4,\"ch\":9},{\"line\":4,\"ch\":13}],[{\"line\":4,\"ch\":14},{\"line\":4,\"ch\":18}],[{\"line\":5,\"ch\":1},{\"line\":5,\"ch\":2}],[{\"line\":2,\"ch\":5},{\"line\":2,\"ch\":13}],[{\"line\":5,\"ch\":7},{\"line\":5,\"ch\":15}],[{\"line\":7,\"ch\":16},{\"line\":7,\"ch\":24}],[{\"line\":6,\"ch\":1},{\"line\":6,\"ch\":2}],[{\"line\":6,\"ch\":7},{\"line\":6,\"ch\":9}],[{\"line\":7,\"ch\":1},{\"line\":7,\"ch\":2}],[{\"line\":7,\"ch\":9},{\"line\":7,\"ch\":24}],[{\"line\":8,\"ch\":1},{\"line\":8,\"ch\":2}],[{\"line\":8,\"ch\":9},{\"line\":8,\"ch\":15}],[{\"line\":10,\"ch\":9},{\"line\":10,\"ch\":15}],[{\"line\":9,\"ch\":0},{\"line\":9,\"ch\":2}],[{\"line\":9,\"ch\":9},{\"line\":9,\"ch\":13}],[{\"line\":10,\"ch\":0},{\"line\":10,\"ch\":2}],[{\"line\":8,\"ch\":9},{\"line\":8,\"ch\":15}],[{\"line\":10,\"ch\":9},{\"line\":10,\"ch\":15}],[{\"line\":11,\"ch\":0},{\"line\":11,\"ch\":2}],[{\"line\":12,\"ch\":0},{\"line\":12,\"ch\":2}]]"}
!@%STYLE%@!
Follow the three declarations of `i` to see why the innermost visible name refers to the file-scope object. section 13.2
TAKEAWAY: An identifier’s scope is the part of a program where its name is visible. Visibility begins at its declaration. A declaration is bound to the scope in which it appears; consistent declarations can describe the same identifier, while contradictory declarations in the same scope are generally disallowed. section 2.2 section 13.2
The enclosing syntax sets the ordinary boundaries:
- A name declared inside a function block has block scope, ending at the relevant closing brace. section 2.2
- A name declared in a `for` construct has visibility limited to that construct and its associated block. section 2.2
- A name declared outside a block has file scope, from its declaration to the end of the file; such names are often called globals. section 2.2
TAKEAWAY: A declaration of the same name in a subordinate scope can shadow an outer name. This hides the outer identifier, not necessarily its object: a saved pointer can still provide access to that object. Keep visibility separate from lifetime, too. Local variables cease to be alive when their function call ends, while each recursive call has its own newly initialized locals and parameters. section 13.2 section 7.3




















