extern storage class specifier for variables

Discussion in 'C Programming' started by ravi_shankar, Jul 4, 2003.

  1. ravi_shankar

    ravi_shankar Guest

    hi all.
    what's the advantage of prefixing a varible with "extern" storage class
    specifier other than its default initializion .
    you can mail to me :


    --
    Posted via http://dbforums.com
    ravi_shankar, Jul 4, 2003
    #1
    1. Advertising

  2. ravi_shankar

    Zoran Cutura Guest

    ravi_shankar <> wrote:
    >
    > hi all.
    > what's the advantage of prefixing a varible with "extern" storage class
    > specifier other than its default initializion .


    The extern specifier doesn't make the variabel initialize. All variables
    with file scope (that is all that are not within block scope) are
    initialized to 0 if there is no explicit initialization.

    Extern tells the compiler that you are not defining the variable here,
    but rather declaring that this variable exists somewhere else in another
    translation unit and you are referring to that.

    > you can mail to me :
    >


    You post here, you read here.

    --
    Z ()
    "LISP is worth learning for the profound enlightenment experience
    you will have when you finally get it; that experience will make you
    a better programmer for the rest of your days." -- Eric S. Raymond
    Zoran Cutura, Jul 4, 2003
    #2
    1. Advertising

  3. ravi_shankar

    Jun Woong Guest

    "Zoran Cutura" <> wrote in message news:be41vr$1jf$-felb.debis.de...
    > ravi_shankar <> wrote:

    [...]
    >
    > Extern tells the compiler that you are not defining the variable here,
    > but rather declaring that this variable exists somewhere else in another
    > translation unit and you are referring to that.


    Provided that the (file scope) declaration has no initializer; but
    it's very bad to provide both of "extern" and an initializer to a
    file scope declaration.


    --
    Jun, Woong ()
    Dept. of Physics, Univ. of Seoul
    Jun Woong, Jul 4, 2003
    #3
  4. In 'comp.lang.c', ravi_shankar <> wrote:

    > what's the advantage of prefixing a varible with "extern" storage class
    > specifier other than its default initializion .


    It's ignored when you define the variable, but it allows a separated
    declaration of this variable when you want it to be shared in different
    modules.

    1 - Are you sure you need a global scope variable?
    2 - If so, define it in a source file (say data.c)
    3 - Declare it in a header (say data.h>
    4 - For consitency, include this header in th definition file (data.c) and
    all the files that need a reference to this variable.
    5 - For readability and maintenance, I suggest that the global scope
    variables are prefixed with G_

    /* data.h */
    #ifndef H_DATA
    #define H_DATA
    extern int G_myglobal;
    extern int G_myarray[123];
    #endif

    /* data.c */
    #include "data.h"

    int G_myglobal;
    int G_myarray[];

    --
    -ed- [remove YOURBRA before answering me]
    The C-language FAQ: http://www.eskimo.com/~scs/C-faq/top.html
    <blank line>
    FAQ de f.c.l.c : http://www.isty-info.uvsq.fr/~rumeau/fclc/
    Emmanuel Delahaye, Jul 4, 2003
    #4
  5. ravi_shankar

    Artie Gold Guest

    ravi_shankar wrote:
    > hi all.
    > what's the advantage of prefixing a varible with "extern" storage class
    > specifier other than its default initializion .


    Prefixing a variable declaration with `extern' says: "This variable is
    defined in another compilation unit." Nothing more, nothing less.

    > you can mail to me :
    >
    >

    Post here. Read here.

    HTH,
    --ag


    --
    Artie Gold -- Austin, Texas



    ----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
    http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
    ---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---
    Artie Gold, Jul 4, 2003
    #5
  6. ravi_shankar

    Dan Pop Guest

    In <> ravi_shankar <> writes:


    > what's the advantage of prefixing a varible with "extern" storage class
    >specifier other than its default initializion .


    Without extern, the declaration becomes a definition (in the absence
    of another definition for that identifier, in that file).

    With extern, the declaration remains a declaration.

    Dan
    --
    Dan Pop
    DESY Zeuthen, RZ group
    Email:
    Dan Pop, Jul 4, 2003
    #6
  7. On Fri, 04 Jul 2003 11:43:39 +0000, in comp.lang.c , ravi_shankar
    <> wrote:

    >
    >hi all.
    > what's the advantage of prefixing a varible with "extern" storage class
    >specifier other than its default initializion .


    sounds like a homework question...
    Anyway, an extern variable is one that is defined elsewhere.

    > you can mail to me :
    >


    post here, read here.

    --
    Mark McIntyre
    CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
    CLC readme: <http://www.angelfire.com/ms3/bchambless0/welcome_to_clc.html>


    ----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
    http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
    ---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---
    Mark McIntyre, Jul 4, 2003
    #7
    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. ravi_shankar

    default storage class specifier of a variable

    ravi_shankar, Jul 5, 2003, in forum: C Programming
    Replies:
    1
    Views:
    469
    Jack Klein
    Jul 5, 2003
  2. aruna
    Replies:
    14
    Views:
    1,876
    Case -
    Apr 29, 2004
  3. sarathy
    Replies:
    2
    Views:
    645
    sarathy
    Jul 17, 2006
  4. sunny

    why Typedef is storage class specifier?

    sunny, Oct 16, 2006, in forum: C Programming
    Replies:
    1
    Views:
    404
    Keith Thompson
    Oct 16, 2006
  5. Sourabh Daptardar

    Storage class specifier of class members

    Sourabh Daptardar, Jun 5, 2008, in forum: C++
    Replies:
    4
    Views:
    327
    James Kanze
    Jun 7, 2008
Loading...

Share This Page