The following compiles:
static int foo() { return 1; }
int foo();
But, will it always compile? Is the behavior in this case well defined? And what does it means when a non-static prototype follows a static declaration?
The following compiles:
static int foo() { return 1; }
int foo();
But, will it always compile? Is the behavior in this case well defined? And what does it means when a non-static prototype follows a static declaration?
Yes it will compile and behavior is well defined. Since
foo
is declaredstatic
earlier thanint foo();
1,foo
has internal linkage.C11: 6.2.2 Linkages of identifiers (p4):
and the foot note states that:
1. If no storage class is specified , the function is assumed to have external linkage. Standard says: 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
-- 6.2.2 (p5).By default functions are global. So making it
The function
foo()
is just visible within this file. Since you just have the declarationint foo();
this is fine and well defined if you have a definition for the sameint foo(){ return 2;}
then you will get redefinition error.As stated by @haccks
Note the difference between declaration and definition.