To follow up with rcgldr, when you call function f() on line 12, you do not inherit the scope where the function was called. You inherit the scope of the function definition. Since f is defined at global/file scope (the only place it can be -- C doesn't allow functions within functions), they only i within it's scope is the global i. The i defined on line 11 exists only within the scope of the curly brackets on lines 10-14 and shadows the global from the point of it's definition. Note, however that this "shadowing" only happens after i has been (re)defined (i.e. only lines after the definition), not for the whole scope. To illustrate this, you can swap the printf on line 9 and the curly brace on line 10, for the following:
Code:
$ cat foo.c
#include <stdio.h>
int i = 1;
void f()
{
i = 2;
}
int main()
{
{
printf("%d\n", i); // global i from within the inner scope
int i = 3;
f();
printf("%d\n", i); // i declared 2 lines above -- shadowing the global
}
printf("%d\n", i); // global i again, outside the scope of i's redefinition
}
$ ./foo
1
3
2