I reckon this has been probably answered before. Please mark as a duplicate if it has!
I'm having trouble understanding what extern
means for local variables (or local functions!). I can't seem to see how it is functionally different from a static
local variable... For instance:
int foo(void) {
extern int i;
return i++;
}
int bar(void) {
static int i;
return i++;
}
AFAICT, foo
and bar
are the same. The difference according to the C99 spec is that i
in foo
should have external linkage compared to the i
in bar
. That said, I can't think of any way to exploit that external linkage - since it is a local variable, it isn't accessible outside of foo
, let alone outside of the file.
I'm sure I'm missing something here - what is it?
EDIT
Thank you to everyone who pointed out that I'm missing a type on extern
and static
. You are quite correct. As a frequent answerer on SO, I now understand the frustration of the barrage of down votes that follow this sort of oversight.