tags:

views:

76

answers:

2

K&R says:

by default external variables and functions have the property that all references to them by the same name, even from functions compiled separately, are references to same thing

Please explain what this means, I don't understand it

+2  A: 

Consider two functions:

extern int extern_sqr(int i) { return i * i; }
static int static_dbl(int i) { return i * 2; }

Then people who refer to extern_sqr will be referring to that function. This is opposed to static linkage, where only people from within the "translation unit" (roughly the file it's defined) can access the function static_dbl.

It turns out, that the extern is implied by default in c. So, you would get the same behavior, if you wrote:

int extern_sqr(int i) { return i * i; }

Newer C standards still require a "function declaration" so, usually in a header file somewhere, you'll encounter:

int extern_sqr(int i);  // Note: 'i' is optional

Which says "somewhere, in some other translation unit, I have a function called extern_sqr.

The same logic applies to variables.

Stephen
You may also add that in the function declaration, the variable names are optional as you may seeint extern_sqr(int);instead.
Maximus
@Maximus : edited, thanks. I could also go into parameter types being _completely_ optional, depending on which standard, but why confuse things :)
Stephen
A: 

external variables and functions are global, i.e. hold the same values (for variables) or definitions (for functions) even when called from different *.c files within your program.

Darel