Search code examples
cextern

Declaring a function as extern


Is there any difference between declaring a function (bar) this way:

char *foo(char *pch)
{
    extern char *bar(); /* this line here */
    ...
}

Or this way?

char *foo(char *pch)
{
    char *bar(); /* this line here */
    ...
}

Solution

  • The 2011 C Standard says in 6.2.2/5:

    If the declaration of an identifier for a function has no storage-class specifier, its linkage is determined exactly as if it were declared with the storage-class specifier extern.

    So there is no technical difference.

    But as already noted in the comments, both are considered bad style. A function declaration does not belong inside another function where it will be used. If you use that pattern and want to change the declaration of the function, you would need to find and modify all the places it's used! A function with external linkage should be declared in a header file. A function with internal linkage (using the static keyword) should be declared somewhere near the beginning of a source file.