const and extern...

Discussion in 'C Programming' started by Shraddha, May 27, 2007.

  1. Shraddha

    Shraddha Guest

    When I read about volatile keyword....while explaning there was an
    declaration like...
    extern const a;

    But as a rule const shoule be initialised where it is defined...but we
    do ot initialise extern where we declare it....So what is exactly
    happening here?
    Shraddha, May 27, 2007
    #1
    1. Advertising

  2. Shraddha

    Ian Collins Guest

    Shraddha wrote:
    > When I read about volatile keyword....while explaning there was an
    > declaration like...
    > extern const a;
    >
    > But as a rule const shoule be initialised where it is defined...but we
    > do ot initialise extern where we declare it....So what is exactly
    > happening here?
    >

    It's being declared. It must be defined once in the application.

    --
    Ian Collins.
    Ian Collins, May 27, 2007
    #2
    1. Advertising

  3. Shraddha

    Chris Torek Guest

    In article <>
    Shraddha <> wrote:
    >... there was an declaration like...
    >extern const a;
    >But as a rule const shoule be initialised where it is defined...


    Yes.

    >but we do not initialise extern where we declare it....So what
    >is exactly happening here?


    This is, quite simply, not a definition.

    In C, when you name a variable's existence, you can *define* the
    variable:

    int foo = 42;

    or merely *declare* the variable:

    extern float bar;

    If you define it, this also declares it (every definition is also
    a declaration), but if you merely declare it, you do not define it.

    For variables declared outside a function, the declaration is also
    a definition if the variable is initialized, but is normally just
    a declaration if the "extern" keyword is used. (If you do both --
    write the "extern" keyword, then initialize it anyway -- you get
    a definition, so:

    extern int foo = 42;

    means the same thing as the first definition above.)

    There is a third state, sort of between the two, called a "tentative
    definition". You get this when you omit the "extern" keyword, but
    also omit the initializer:

    char *tentative;

    In this case, the variable is "tentatively defined" from that point
    forward in the translation unit, up until the compiler sees a
    (non-tentative, for-sure for-real) definition:

    char *zorg; /* initially, tentative */
    char *zorg = "evil"; /* now, defined */

    or the translation unit ends. If the translation unit ends and no
    definition has occurred, the effect is the same[%] as if you had added,
    just at the end, an actual definition with an initializer of {0}:

    char *tentative;
    /* if the file ends, now, this means:
    char *tentative = {0};
    which makes it initially NULL. */

    Again, these rules apply ONLY to file-scope variables (those outside
    function blocks). Inside a function, there is no such thing as a
    "tentative definition":

    void f(void) {
    int x; /* actually defines x, which contains no useful value,
    so be sure to give it one before using it */
    }

    -----
    [%] In some implementations, "tentative definition" variables are
    treated specially, and multiple tentative definitions in separate
    translation units are simply merged at link time. The C Standards
    (C89 and C99 both) say that this situation is undefined, so
    implementations can define it, if they like. Other implementations
    make it an error. One popular implementation (GCC) can be told
    to behave either way.
    --
    In-Real-Life: Chris Torek, Wind River Systems
    Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
    email: forget about it http://web.torek.net/torek/index.html
    Reading email is like searching for food in the garbage, thanks to spammers.
    Chris Torek, May 27, 2007
    #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. Thomas Matthews
    Replies:
    5
    Views:
    2,373
    tom_usenet
    Aug 2, 2004
  2. Replies:
    11
    Views:
    1,089
  3. Javier
    Replies:
    2
    Views:
    548
    James Kanze
    Sep 4, 2007
  4. 0m
    Replies:
    26
    Views:
    1,095
    Tim Rentsch
    Nov 10, 2008
  5. Andre
    Replies:
    5
    Views:
    522
    Keith Thompson
    Jul 17, 2012
Loading...

Share This Page