entering main x: 10 entering extern bar x: 10 exiting extern bar x: 20 entering foo. static bar. exiting foo. exiting main x: 40
Comments:
Declaring a function static
enables you to use a name that might otherwise conflict
with the name of a function in another file.
The function name bar
in fileB.c
is not external,
(because static
gives it internal linkage),
so is not linked ot the bar
that main
uses.
/* --- fileA.c --- */ int x = 10;
/* --- fileB.c --- */ #include <stdio.h> int x; static int bar(int val) { printf(" static bar.\n"); return 2*val; } void foo() { printf(" entering foo.\n"); x = bar(x); printf(" exiting foo.\n"); }
/* --- fileC.c --- */ #include <stdio.h> void foo(); int x; void bar() { printf(" entering extern bar x: %d\n", x); x += 10; printf(" exiting extern bar x: %d\n", x); } void main() { printf("entering main x: %d\n", x ); bar(); foo(); printf("exiting main x: %d\n", x ); }