Use of extern

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • polas

    #1

    Use of extern

    I have a quick question - I have used extern quite a bit in relation
    to variables, however was wondering if its applicable to function
    prototypes too?

    For instance, I have a file called a.c - a small snapshot of it
    contains

    extern int a;
    void hello(int);

    Then in a file called e.c, a small snapshot I have

    int a;
    void hello(int num)
    {
    .....
    }

    These two C files are linked together during compilation, but is there
    any reason to add extern in front of the hello function prototype in
    a.c (or would adding this not conform to the standard.) On my C
    compiler (gcc) it doesnt complain when I add extern in this way, but
    doesnt seem to do a huge amount.

    Cheers,
    Nick
  • Nick Keighley

    #2
    Re: Use of extern

    On 24 Oct, 12:08, polas <n...@helpforce .comwrote:
    I have a quick question - I have used extern quite a bit in relation
    to variables, however was wondering if its applicable to function
    prototypes too?
    sort of
    For instance, I have a file called a.c - a small snapshot of it
    contains
    >
    extern int a;
    void hello(int);
    >
    Then in a file called e.c, a small snapshot I have
    >
    int a;
    void hello(int num)
    {
    ....
    >
    }
    >
    These two C files are linked together during compilation, but is there
    any reason to add extern in front of the hello function prototype
    only for documentaion reasons
    in
    a.c (or would adding this not conform to the standard.)
    adding extern to a function declaration does conform to
    the standard

    On my C
    compiler (gcc) it doesnt complain when I add extern in this way, but
    doesnt seem to do a huge amount.
    correct.

    function declaration
    void hello(int);

    function definition:
    void hello(int n)
    {
    }

    by default a function definition is has external linkage
    (is "visible" throughout the program) if you put static
    on the front it only has file scope (it is only "visible"
    to the file it is definied in). Adding extern is the same as
    not using static. Hence some people use extern on functions
    and many do not. I don't use extern on functions.


    --
    Nick Keighley

    "The quality I have in mind is all-absorbing,
    not just a way of doing things but a way of being."

    Comment

    • Ben Bacarisse

      #3
      Re: Use of extern

      polas <nick@helpforce .comwrites:
      I have a quick question - I have used extern quite a bit in relation
      to variables, however was wondering if its applicable to function
      prototypes too?
      Yes, but extern is the default for file-scope function declarations.
      It is usual to omit it. You need it if you write a function
      declaration at block scope but that is not commonly done.

      --
      Ben.

      Comment

      • Malcolm McLean

        #4
        Re: Use of extern


        "polas" <nick@helpforce .comwrote in message news:
        >I have a quick question - I have used extern quite a bit in relation
        to variables, however was wondering if its applicable to function
        prototypes too?
        >
        Ideally you'd have a hierarchy of exports so you could have a C source file
        that was local to a library, but shared within the library.
        Unfortunately you can't do this. extern is a bit like "auto".

        --
        Free games and programming goodies.


        Comment

        Working...