As I know it, objects in C have 3 types of linkages: 1)external 2)internal and 3)none, and that objects declared at block scope, as within a function body, have no linkage unless preceded with the keyword "extern" or "static".
But why then the function declaration below is able to link to the definition below the main() function even though I've not used "extern" during declaration? Please explain this as it is throwing my whole understanding of the topic upside down. Thank you.
#include<stdio.h>
int main()
{
int foo(); //working even though I've not used "extern"
printf("%d",foo());
}
int foo()
{
return 8;
}
RESULT OF ABOVE PROGRAM: 8
and that objects declared at block scope, as within a function body, have no linkage unless preceded with the keyword "extern" or "static".
Functions are not objects.
6.2.2 in C11 says
-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.
THe first sentence says a function declared at file scope is as if declared with extern
. That applies even if declared at block scope. The next paragraph is:
-6- The following identifiers have no linkage: an identifier declared to be anything other than an object or a function; an identifier declared to be a function parameter; a block scope identifier for an object declared without the storage-class specifier
extern
.
That says block-scope objects have no linkage, but not functions.
You can't have nested functions in ISO C, so it would make no sense to be able to declare a block-scope function if it didn't refer to something outside the block.