In C, if objects declared in block scope have no relationship, then why does declaring a function inside main () without "extern" work?

As I know, objects in C have 3 types of relationships: 1) external 2) internal and 3) none, and objects declared in block scope, as inside the body of a function, have no binding if preceded by the keyword "extern" or " static ".

But why then the function declaration below can refer to the definition below the main () function even though I didn't use "extern" during the declaration? Please explain this as it throws my understanding of the problem upside down. Thank.

#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

+3


source to share


2 answers


and that objects declared in block scope, as within the body of a function, have no links unless the keyword "extern" or "static" is specified.

Functions are not objects.

6.2.2 in C11 says

-5- If an identifier declaration for a function does not have a storage class specifier, its binding is defined exactly as if it were declared with a storage class specifier extern

. If an identifier declaration for an object has file scope and storage class specifier, its relationship is external.



The first sentence says that a function declared in file scope is declared as extern

. This applies even if declared in block scope. Next paragraph:

-6- The following identifiers are unbound: an identifier declared as something other than an object or function; an identifier declared as a function parameter; block scope identifier for an object declared without a storage class specifier extern

.

This suggests that block scope objects have no binding, but no function.

You cannot have nested functions in ISO C, so it would be pointless to declare a block-scope function unless it was referencing something outside the block.

+5


source


Functions are not objects. So what you are talking about objects is not about functions.



+1


source







All Articles