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

2

12

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.

Panther answered 16/10, 2020 at 11:30 Comment(1)
Linkage is a property of identifiers, not of functions or objects. Distinguishing these correctly can aide understanding of the concepts.Clerk
S
8

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.

Subrogation answered 16/10, 2020 at 11:46 Comment(4)
Got it. Just one doubt. The standard says : For an identifier declared with the storage-class specifier extern in a scope in which a prior declaration of that identifier is visible. In my program won't that scope be main() ? You are talking about file scope. Isn't the file scope different from main scope ?Panther
@Panther Yes the scope of the function declared in main is the outmost block scope of main and in this scope a previous declaration of the function is not visible. That is the function declaration in main does not redeclare a function with the same type in the outer scope relative to the main's scope.Subrogation
I am not getting it. Let's say if static void f_in_other_place (void); this declaration is present before my main()'s declaration, will it be visible to my later declaration ? and than it will change the linkage to internal. Am i understanding it right ??Panther
@Panther This declaration in the file scope is visible in main because neither other declaration redeclares this identifier until the declaration of the function in main. So the declaration in main declares the same one function with the declaration in the file scope.Subrogation
S
4

From this linkage reference:

external linkage. The identifier can be referred to from any other translation units in the entire program. All non-static functions, all extern variables (unless earlier declared static), and all file-scope non-static variables have this linkage.

[Emphasis mine]

It doesn't matter where you declare the function, it will always have external linkage.

Suppository answered 16/10, 2020 at 11:35 Comment(2)
So is that true that all declarations have implicit extern prefixed ? and there is no way you can force a function to have none linkage ?Panther
@Panther No linkage means it's local in the scope it's declared in. And you can't have functions inside other functions which makes it impossible to have no linkage. A function can only have external or internal linkage.Suppository

© 2022 - 2025 — McMap. All rights reserved.