extern and static

Discussion in 'C++' started by ik, Sep 22, 2004.

  1. ik

    ik Guest

    Hello all,
    Under C++ when 'extern' is used with out the string literal "C" does
    it act the same as a 'static' ? When is it appropriate using 'extern' ?
    Thanks
    ~Ik
    ik, Sep 22, 2004
    #1
    1. Advertising

  2. ik

    JKop Guest

    ik posted:

    > Hello all,
    > Under C++ when 'extern' is used with out the string

    literal "C" does
    > it act the same as a 'static' ? When is it appropriate

    using 'extern' ?
    > Thanks
    > ~Ik
    >
    >


    When you write:

    int k;

    Then that's going to define a variable.

    But what if you don't want to define a variable, what if
    this variable has already been defined in some other source
    file and you just want to use it in this source file. Well
    here's how you say that you *don't* want to define a
    variable, you just want to use one that's already been
    defined (sort of like a function prototype):

    extern int k;


    -JKop
    JKop, Sep 22, 2004
    #2
    1. Advertising

  3. ik wrote in news: in
    comp.lang.c++:

    > Hello all,
    > Under C++ when 'extern' is used with out the string literal "C" does
    > it act the same as a 'static' ?


    No extern is used to declare that something is defined elsewhere possibly
    in another TU (TU = Translation Unit, a source file or library etc).

    static has several meanings:

    1) Used in a class/struct/union it declares/defines that the class
    (not its instances) has a member object or function.

    2) Used at namespace scope (*) or within a function it declares/defines
    an object or function (namespace scope only) that has internal
    linkage, i.e. other TU's (sources) will not be able to access
    this object (as it doesn't have an "extern" name).

    Within a function it also means the object will live from the
    time it is first initialized (when the function is first called)
    to the end of the programme.

    *) namespace scope: within a namespace block:

    namespace name { /* here */ }

    or at global scope, not in a namespace, class or function.

    > When is it appropriate using 'extern' ?


    extern is mostly useful for objects that you want to declare but
    not define:

    int x;
    extern int y;

    In the above x is declared and defined, y is only declared, so
    in another TU (source) y could be defined:

    int y = 2;

    However both x and y have external linkage, its the default.

    extern also needs to be used with const's that you want to have
    external linkage, as const object's have internal linkage by
    default (i.e. they are static):

    int const a = 3;
    extern int const b;
    extern int const c = 4;

    In the above a has internal linkage, b has external linkage
    and will need to be defined elsewhere:

    extern int const b = 2;

    and c has external linkage, but because it has an intializer
    it is also a defenition.

    Declaration's are extern by default so, with function's:

    int f();

    is equivalent to:

    extern int f();

    The only time you need to use extern with a function is
    when you want to give it a linkage specification:

    extern "C" int g();

    HTH.

    Rob.
    --
    http://www.victim-prime.dsl.pipex.com/
    Rob Williscroft, Sep 22, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. A
    Replies:
    5
    Views:
    3,405
    Victor Bazarov
    Nov 27, 2003
  2. Thomas Matthews
    Replies:
    5
    Views:
    2,367
    tom_usenet
    Aug 2, 2004
  3. Dominik Rau
    Replies:
    3
    Views:
    680
    Dominik Rau
    Apr 25, 2005
  4. Magix

    extern and Static

    Magix, Oct 6, 2004, in forum: C Programming
    Replies:
    5
    Views:
    649
    Ben Pfaff
    Oct 9, 2004
  5. Andre
    Replies:
    5
    Views:
    517
    Keith Thompson
    Jul 17, 2012
Loading...

Share This Page