Is extern keyword for function necessary at all in C?

前端 未结 4 1209
滥情空心
滥情空心 2020-12-05 11:25

It appears to me that even if I refer to a function in another file with out extern declaration, gcc can still compile that unit. So I am wondering whether the extern declar

相关标签:
4条回答
  • 2020-12-05 11:38

    As far as I remember the standard, all function declarations are considered as "extern" by default, so there is no need to specify it explicitly. That doesn't make this keyword useless since it can also be used with variables (and it that case - it's the only solution to solve linkage problems). But with the functions - yes, it's optional.

    A little more verbose answer is that it allows you to use variables compiled in another source code file, but doesn't reserve memory for the variable. So, to utilise extern, you have to have a source code file or a library unit that contains memory space for the variable on the top level (not within functions). Now, you can refer to that variable by defining an extern variable of the same name in your other source code files.

    In general, the use of extern definition should be avoided. They lead easily to unmanagable code and errors that hard to locate. Of course, there are examples where other solutions would be impractical, but they are rare. For example, stdin and stdout are macros that are mapped to an extern array variable of type FILE* in stdin.h; memory space for this array is in a standard C-library unit.

    0 讨论(0)
  • 2020-12-05 11:39

    functions have extern storage class specifier by default (unless they are explicitly defined as static)

    extern Storage Class Specifier

    If the declaration describes a function or appears outside a function and describes an object with external linkage, the keyword extern is optional. If you do not specify a storage class specifier, the function is assumed to have external linkage.

    ....

    It is an error to include a declaration for the same function with the storage class specifier static before the declaration with no storage class specifier because of the incompatible declarations. Including the extern storage class specifier on the original declaration is valid and the function has internal linkage.

    0 讨论(0)
  • 2020-12-05 11:47

    You do not necessarily "need" extern for variables.

    When C was invented Unix linkers were also written, and they advanced the art in unheralded but clever ways. One contribution was defining all symbols as small "common blocks". This allowed a single syntax for declarations with no required specification of which module was allocating the space. (Only one module could actually initialize the object, but no one was required to.)

    There are really three considerations.

    1. Forward declarations for prototypes. (Optional, because legacy C has to compile without them.)

    2. Extern declarations for non-function objects (variables) in all files except one. (Needed only on non-Unix systems that also have crummy linkers. Hopefully this is rare these days.)

    3. For functions, extern is already the assumption if no function body is present to form a definition.

    0 讨论(0)
  • 2020-12-05 11:52

    It's not necessary, but I prefer it in headers to reinforce the idea that this function is defined somewhere else.

    To me, this:

    int func(int i);
    

    is a forward declaration of a function that will be needed later, while this:

    extern int func(int i);
    

    is a declaration of a function that will be used here, but defined elsewhere.

    The two lines are functionally identical, but I use the extern keyword to document the difference, and for consistency with regular variables (where the difference is important, and has exactly that meaning).

    0 讨论(0)
提交回复
热议问题