Search code examples
cfunctionexternalexternlinkage

Will a function declared inside main() have external linkage or none linkage?


See the following code:

/* first file */

int i; /* definition */
int main () {
  void f_in_other_place (void);   /* declaration */
  i = 0
  return 0;
}
/* end of first file */


/* start of second file */

extern int i; /* declaration */
void f_in_other_place (void){   /* definition */
  i++;
}
/* end of second file */

I know that external objects have external linkage and internal objects have none linkage(ignoring extern for a moment). Now if i talk about the function f_in_other_place(), it is declared inside main function. So will the identifier for it be treated as an internal object ? If yes than it should have none linkage but as visible in program this function refers to it's definition in second file which shows that identifier for it is behaving like an object with external linkage. So i am confused whether this identifier here has external linkage or none linkage ?

Now coming to the extern keyword, I read somewhere that function declaration implicitly prefixes extern. So even if i have not mentioned extern for this function identifier explicitly, will my function's identifier by default become an object with external linkage and scoped inside main() ? Please correct me if i am going in wrong direction.


Solution

  • I know that external objects have external linkage and internal objects have none linkage

    I think that by the term "internal objects" you mean objects declared in block scopes.

    As for this declaration

    int i; /* definition */
    

    then it is a declaration. You may place several such declarations one after another like

    int i; /* definition */
    int i; /* definition */
    int i; /* definition */
    

    The compiler generates the so-called tentative definition of this variable at the end of the translation unit initializing it by zero.

    As for the function declaration in main then according to the C Standard (6.2.2 Linkages of identifiers)

    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. If the declaration of an identifier for an object has file scope and no storage-class specifier, its linkage is external.

    and

    4 For an identifier declared with the storage-class specifier extern in a scope in which a prior declaration of that identifier is visible,31) if the prior declaration specifies internal or external linkage, the linkage of the identifier at the later declaration is the same as the linkage specified at the prior declaration. If no prior declaration is visible, or if the prior declaration specifies no linkage, then the identifier has external linkage.

    So this function declaration in main

    void f_in_other_place (void);
    

    is equivalent to

    extern void f_in_other_place (void);
    

    As there is no previous function declaration in the file scope then this function has external linkage.

    If for example in the file scope before main there would be a declaration with the keyword static like

    static void f_in_other_place (void);
    

    then the function declared in main would have internal linkage.