extern question

Discussion in 'C Programming' started by Josh Lessard, Jun 26, 2003.

  1. Josh Lessard

    Josh Lessard Guest

    Hi all. I've read through all the K&R2 chapters (thanks for suggesting
    it...it was a great read) and I'm now reading the appendices.
    Specifically, I'm reading section A11 - Scope & Linkage p227. I have a
    question about the "extern" specifier.

    Section A11.2 Linkage p228 says:

    "All declarations for the same object or function identifier with external
    linkage refer to the same thing, and the object or function is shared by
    the entire program."

    and

    "... the first external declaration for an identifier gives the identifier
    internal linkage if the static specifier is used, external linkage
    otherwise."

    My questions are, is there any point to declaration an external object
    "extern"? Does it have any effect(s)? I'm almost positive I've seen code
    with external declarations that included the keyword "extern", but given
    the quoted statements above, that seems redundant. Am I missing
    something?

    *****************************************************
    Josh Lessard
    Master's Student
    School of Computer Science
    Faculty of Mathematics
    University of Waterloo
    (519)888-4567 x3400
    http://www.cs.uwaterloo.ca
    *****************************************************
     
    Josh Lessard, Jun 26, 2003
    #1
    1. Advertising

  2. Josh Lessard

    Matt Guest


    > My questions are, is there any point to declaration an external object
    > "extern"? Does it have any effect(s)? I'm almost positive I've seen code
    > with external declarations that included the keyword "extern", but given
    > the quoted statements above, that seems redundant. Am I missing
    > something?


    Yes (I'll let you decide which question I'm answering ;-) ), extern
    comes in handy when you have multiple .c or .cpp files that are linked
    together. If you declare it in one it will not be known of in the
    other, and if you declare it in a common .h file, it will have multiple
    definitions (be declared twice and the compiler will scream bloody
    murder). So if you put extern in the common header (or in the .c or .cpp
    file(s) that you didn't declare it in, and declare your variable in only
    on of your .c or .cpp files, then the compiler is happy and you can
    access your variable from everywhere in your program. Please note,
    however that using extern might possibly get you martyred as it is used
    for global variables (AFAIK), and to some people, these hold religious
    significance as the bane of all that is holy and good programming
    practice. I must say, however that there are some places where they are
    handy and some that they are necessary (or at least helpful). I won't
    go any further, lest I start a holy war. Hope I was of help, but I
    didn't see anyone respond to your question, and I hope your still
    checking out the newsgroups as it has been quite a while. You've
    probably already figured out the answer one way or another, but such an
    unanswered question bothers me, so let's just say I did it for my peace
    of mind ;-)
    -Matt
     
    Matt, Jun 9, 2004
    #2
    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. exits funnel

    Question about an extern int

    exits funnel, Dec 7, 2003, in forum: C++
    Replies:
    4
    Views:
    5,316
    Biyou Tang
    Dec 8, 2003
  2. mark

    question about extern ?..

    mark, Jun 8, 2004, in forum: C++
    Replies:
    10
    Views:
    538
    Default User
    Jun 8, 2004
  3. Thomas Matthews
    Replies:
    5
    Views:
    2,547
    tom_usenet
    Aug 2, 2004
  4. "extern" question?

    , Aug 15, 2006, in forum: C Programming
    Replies:
    5
    Views:
    382
  5. Andre
    Replies:
    5
    Views:
    563
    Keith Thompson
    Jul 17, 2012
Loading...

Share This Page