Re: "extern" inside a block

From: G Patel (gaya.patel_at_gmail.com)
Date: 02/23/05


Date: 23 Feb 2005 12:35:33 -0800

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?



Relevant Pages

  • Re: keyword extern
    ... The header should also be included in the definition source file. ... >> This is the only way to check the match between the declaration and the ... term you are looking for here is "file scope" which is well defined by ... One never needs to use the extern keyword with a function definition, ...
    (comp.lang.c)
  • Re: "extern" inside a block
    ... In multi source file programs, ... > I know the behaviour when extern declaration follows a non-extern ... Linkage of Identifiers ... scope in which a prior declaration of that identifier is visible [and ...
    (comp.lang.c)
  • Re: keyword extern
    ... >> definition or declaration until the end of the translation unit being ... >> One never needs to use the extern keyword with a function definition, ... >> ...then here is how a C compiler understands them. ... If any code in the source file access 'x', ...
    (comp.lang.c)
  • Re: Compiling Guppy-PE extension modules
    ... >> The MSDN help gives a simple example of code raising compiler error ... src\sets\sets.c: error C2099: initializer is not a constant ... I don't know if it applies here, but in this context the extern keyword ...
    (comp.lang.python)
  • Re: EXTERN Qualifier
    ... extern const unsigned int x; ... My compiler complains about using extern with 'const'. ... object declaration as it is on a function declaration. ... On an object declaration with an initializer at file scope, ...
    (comp.lang.c)