Eric Sosman wrote:
G Patel wrote:
I've seen things like this too:
{
extern int foo = 10;
/* rest of block */
}
If you've seen them and the compiler didn't complain,
the compiler was being operated in a non-conforming mode:
6.7.8 Initialization
/5/ If the declaration of an identifier has block
scope, and the identifier has external or internal
linkage, the declaration shall have no initializer
for the identifier.
If you've seen `extern int foo = 10;' at file scope,
outside a block, that's fine: it's a definition of `foo'
with an initializer, specifying (unnecessarily) external
linkage. But it's not permitted to do this inside a block.
You're right, I've only seen extern with initializer at the top of a
file. This is weird, so with an intializer it's a definition, without
an initializer it's a declaraction.
I have one more question. In multi source file programs, I put extern
declaractions for variables I want to share amongst many source files
in a headder, and include that in all those source files. I never used
to include that in the file I defined the file-scope variable. I
tried it after a suggestion and it works. But is this 100% proper (not
undefined)?
Technically, after preprocessing that source file has this at the top:
extern T var; /* came in from #include */
T var; /* was in the .c file */
I know the behaviour when extern declaration follows a non-extern
declaraction is okay, because the extern takes the
linkage/characteristics of the non-extern declaration (either static or
implicity external). But how does this work when extern declaration
PRECEDES a non-extern declaration?
Am I safe to include the header like this?