extern keyword

Discussion in 'C Programming' started by Seeker, Jun 29, 2006.

  1. Seeker

    Seeker Guest

    I have some code that #includes a file and declares a function with
    extern whose declaration was also in the included file. Why is this?
    Should we re-declare the functions with extern? Why don't we just use
    the function? Why do we include it then?
    Seeker, Jun 29, 2006
    #1
    1. Advertising

  2. Seeker posted:

    > I have some code that #includes a file and declares a function with
    > extern whose declaration was also in the included file. Why is this?
    > Should we re-declare the functions with extern? Why don't we just use
    > the function? Why do we include it then?


    Short answer: Take out the "extern" -- it isn't doing anything. (Unless
    the functions is "inline", in which case, read... )

    Long answer:

    C has loads of abbreviations all over the place (well, I like to think of
    them as abbreviations). Consider:

    int main()
    {
    long i;
    }


    instead of:

    signed int main(void)
    {
    signed long int auto i;

    return 0;
    }


    Looking at the above snippet, a variable within a function defaults to
    "auto" rather than "static", and a "long int" defaults to signed, rather
    than unsigned.

    There are some keywords in C that get omitted 99.9999992423211244% of the
    time (like "auto" in the previous example).

    When you define a variable within a function, it must be either "auto" or
    "static", i.e.:

    int main(void)
    {
    static int i = 5;

    auto int j = 5;
    }

    If neither keyword is present, then it defaults to "auto".


    Moving on to global variables...


    Global variables, and also functions, must be either "extern" or
    "static".

    "extern" means that they CAN be accessed from other source files.

    "static" means that they can NOT be accessed from other source files.


    If neither keyword is present, then it defaults to "extern". (C++ is
    different in this regard, just so you know...)


    So... the question is, "What use is there for extern?". I'll tell you one
    usage.

    By writing:

    extern int i;

    instead of:

    int i;

    you're saying to the compiler, "I'm not defining an object, I'm just
    forward-declaring one".

    So if you want access to an object which is DEFINED within another source
    file, then you DECLARE the object in the current source file by using the
    keyword "extern".

    (I suppose you could say that the functionality of "extern" is extended
    to indicate that you want a declaration rather than a definition).

    "extern" has no noticible effect whatsoever when placed before a function
    (unless the function is inline... could someone please confirm that?).


    --

    Frederick Gotham
    Frederick Gotham, Jun 29, 2006
    #2
    1. Advertising

  3. It's a common practice for functions that are going to be used in other files. You
    declare it in the header file, and define it in the C file. The C file includes the
    header file so that it any changes are made, you will get appropriate warnings and/or
    errors.


    --
    #include <standard.disclaimer>
    _
    Kevin D Quitt USA 91387-4454 96.37% of all statistics are made up
    Kevin D. Quitt, Jul 3, 2006
    #3
  4. On 2006-07-02, Kevin D Quitt <> wrote:
    > It's a common practice for functions that are going to be used in other
    > files. You declare it in the header file, and define it in the C file.
    > The C file includes the header file so that it any changes are made, you
    > will get appropriate warnings and/or errors.
    >


    That's correct.

    You should quote context. Since you aren't using Google, I assume that you
    deliberately snipped what you replied to. Don't do that. (If you /didn't/
    deliberately snip quotes, you need to find a better newsreader).

    --
    Andrew Poelstra <http://www.wpsoftware.net/blog>
    To email me, use "apoelstra" at the above address.
    "You people hate mathematics." -- James Harris
    Andrew Poelstra, Jul 3, 2006
    #4
  5. Seeker

    Xicheng Jia Guest

    Seeker wrote:
    > I have some code that #includes a file and declares a function with
    > extern whose declaration was also in the included file. Why is this?
    > Should we re-declare the functions with extern? Why don't we just use
    > the function? Why do we include it then?


    This page may answer most of your questions: :)

    http://www.c-faq.com/decl/decldef.html

    Xicheng
    Xicheng Jia, Jul 3, 2006
    #5
  6. Seeker

    Seeker Guest

    Seeker, Jul 3, 2006
    #6
    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,414
    tom_usenet
    Aug 2, 2004
  2. tweak

    keyword extern

    tweak, Jun 26, 2004, in forum: C Programming
    Replies:
    18
    Views:
    650
    Dan Pop
    Jun 29, 2004
  3. ooze

    how to use the keyword extern in c?

    ooze, Aug 25, 2004, in forum: C Programming
    Replies:
    16
    Views:
    642
    Dan Pop
    Aug 30, 2004
  4. siliconwafer

    Use of 'extern' keyword

    siliconwafer, Jul 28, 2005, in forum: C Programming
    Replies:
    5
    Views:
    380
    Kenny McCormack
    Jul 31, 2005
  5. Andre
    Replies:
    5
    Views:
    537
    Keith Thompson
    Jul 17, 2012
Loading...

Share This Page