Re: extern



Me wrote:
> DevarajA wrote:
> > Can anyone explain me what extern is used for? I thought it was used to
> > declare variables definited in other files, but i can do that also
> > without extern.
>
> I'd *highly* suggest you use the extern keyword when declaring external
> variables as opposed to just using:
>
> int a;
>
> because this looks like you're defining a global variable that gets
> zero initialized as opposed to declaring a global variable defined in
> another translation unit
>
> > /*file a.c*/
> > int a=5;
> > int main()
> > {
> > f();
> > }
> >
> > /*file b.c*/
> > int a;
> > void f()
> > {
> > printf("%d\n",a); /*it prints 5*/
> > }

Ok, I'm talking crap here based on information I vaguely remember from
something I read a long time ago (it probably had something to do with
an ancient unix compiler). What you're doing in b.c is called a
tentative definition:

6.9.2/2 "A declaration of an identifier for an object that has file
scope without an initializer, and without a storage-class specifier or
with the storage-class specifier static, constitutes a tentative
definition. If a translation unit contains one or more tentative
definitions for an identifier, and the translation unit contains no
external definition for that identifier, then the behavior is exactly
as if the translation unit contains a file scope declaration of that
identifier, with the composite type as of the end of the translation
unit, with an initializer equal to 0."

So as you can see, Jack Klein's answer was right about undefined
behavior occuring if you link these two files together because in b.c,
the compiler acts as if you have:

int a = 0;

at the very bottom.

.



Relevant Pages

  • Re: extern
    ... declare variables definited in other files, but i can do that also without extern ... For objects it is a "tentative declaration" NOT an extern, although it has external linkage. ... If nothing else is seen by the end of the translation unit then it is as if there was a declaration with a 0 initialiser at the end of the translation unit. ... " 6.9.2 External object definitions Semantics 1 If the declaration of an identifier for an object has file scope and an initializer, the declaration is an external definition for the identifier. ...
    (comp.lang.c)
  • Re: frequently asked C and C++ interview questions along with their answers
    ... The extern behaviour I said is an inherited ... > If the declaration of an identifier for an object has file scope ... the declaration is an external definition for the ... If a translation unit contains one or more tentative ...
    (comp.lang.cpp)
  • Re: Yet another brute force sudoku solver
    ... it) since anyone can declare an extern and then see it. ... declaring the same identifier with external ... linkage in a different translation unit will not make this ... definition visible in that translation unit. ...
    (comp.lang.c)
  • Re: Yet another brute force sudoku solver
    ... it) since anyone can declare an extern and then see it. ... declaring the same identifier with external ... linkage in a different translation unit will not make this ... definition visible in that translation unit. ...
    (comp.lang.c)
  • Re: using libc from nasm
    ... I am trying to learn asm with NASM on a FreeBSD system. ... I don't think declaring these "extern" in your file is going to help. ...
    (comp.lang.asm.x86)