E30 Answer


x in main: 999
x in foo: 10
x in foo: 20
x in bar: 20
x in bar: 40
x in main: 999

/* --- fileA.c --- */
int x = 8;    /* external linkage, file scope */


/* --- fileB.c --- */ static int x = 10 ; /* internal linkage, file scope */ foo( ) { printf("x in foo: %d\n", x ); x = x+10 ; printf("x in foo: %d\n", x ); } bar( ) { printf("x in bar: %d\n", x ); x = 2*x ; printf("x in bar: %d\n", x ); }
/* --- fileC.c --- */ static int x = 999; /* internal linkage, file scope */ main() { printf("x in main: %d\n", x ); foo(); bar(); printf("x in main: %d\n", x ); system("pause"); }

Comments: main() sees the x in its file that has internal linkage. The two functions in fileB.c both see the x in that file that has internal linkage (which is a different x from what code>main() sees. There is an external object, also named x, but no function uses it.