1

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 */
    ...
}
Bite Bytes
  • 1,455
  • 8
  • 24
  • 3
    No difference, both are poisonous and should be treated with fire. Extern function declarations belong in header files, not inside functions. – n. m. could be an AI Sep 04 '17 at 16:54
  • 2
    In general, don't declare functions inside other functions. Functions that need a declaration (all functions visible outside the source file that defines them) should be declared in a header and code using that function should include the header — and so should the code defining the function. Functions that are local to a single source file (`static`) should also be declared (or defined) at file scope (C doesn't support nested functions, despite what GCC does) before they are used. – Jonathan Leffler Sep 04 '17 at 17:01
  • @JonathanLeffler why the keyword `static` has been chosen for both linkage and storage class, wouldn't be less confusing for example to use `intern` for internal linkage? – Bite Bytes Sep 04 '17 at 17:03
  • 1
    History — there is no explanation other than "that is the way the DMR designed it, or the way it evolved". You could have had 'public' and 'private' and all sorts of other keywords, but `static` was chosen and became enshrined by use before it could be changed. – Jonathan Leffler Sep 04 '17 at 17:05
  • 1
    You might also note that neither declaration in the question is a prototype; the function `bar` can be called with any number of arguments of any type and the compiler will accept them (probably without complaint). If the function takes no argument, say so: `extern char *bar(void);`. The `extern` is implied — all function declarations inside a function are implicitly (inherently?) `extern`. But the functions shouldn't be declared inside other other functions. Regard declaring functions inside other functions as an obsolescent feature (along with the absence of a prototype). – Jonathan Leffler Sep 04 '17 at 17:09
  • @PeterJ_01 my question is about using the keyword `extern` VS not using it when declaring a function inside another. – Bite Bytes Sep 04 '17 at 17:19
  • What is the difference? it means exactly the same – 0___________ Sep 04 '17 at 17:20

1 Answers1

1

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.

aschepler
  • 70,891
  • 9
  • 107
  • 161
  • Sometimes it comes in handy when you have to choose between calling one of many functions, and you don't know which one until runtime. for example you have functions called `shape *create_triangle() `, `shape *create_rectangle()`, `shape *create_circle()`, so one can declare inside the function a variable called `shape * (*create_shape)()`, and set it to the appropriate function address according to some input at runtime. – Bite Bytes Sep 04 '17 at 18:07
  • 1
    @BiteBytes That would be defining a function pointer, not declaring a function. – aschepler Sep 04 '17 at 18:14