Reputation: 6762
I have this snippet of code:
int foo1(void); //line a
int foo2(void) {
return foo1();
}
int foo1(void) { //line b
return 99;
}
If I want to declare function foo1 to be static, should I put the keyword static in line a or line b? Is there any difference?
Also say I have the following code in ANOTHER file that uses foo1 in the above file:
static int foo1(void);
int main(void) {
return foo1();
}
The code still compiles and works as expected although I put static in the declaration line. But it gives a warning saying that 'foo1' used but never defined - if it's not defined, why does the code still work?
Edit: Sorry I didn't make that clear, for the second question, foo1 in the first file where it's defined is not declared as static, but I declare it as static in the second file.
Upvotes: 1
Views: 142
Reputation: 3325
In C static for functions means internal linkage and use of the function only within the scope of the file. Interesting that compilers behave differently:
Snippet 1:
GCC and clang requires putting static
in declaration (line a), in definition (line b) it can b omitted then.
In MS VC you can put static
in declaration or in definition only, this makes foo1() static.
Snippet 2 (static foo1 in main.c, non-static declaration in other file): GCC compiles static declaration without definition. clang compiles but outs a warning. (Both link then.) MS VS does not compile the second snippet at all, giving an error C2129.
So, put static
in both declaration and definition for clarity, and define static functions in the files where they are declared. Put forward declarations if required before usage of the functions.
Overall, seems that clang has better diagnostics.
"...if it's not defined, why does the code still work?"
In C it is not required to declare prototype before function call. The definition is than found on the linkage step.
Upvotes: 0
Reputation: 42109
The static
keyword in this context specifies that foo1
not be exported, so that particular foo1
could only be defined in that same compilation module, hence the warning when it isn't. (Without the static
the code would be correct, assuming you mean to link the foo1
from the other file.)
As for why it works regardless of this, my guess is that the compiler falls back on looking for the external function, which it finds from your other file, but I think a conforming compiler would be justified in giving an error here.
Upvotes: 1
Reputation: 11961
In the first case, you need to put static
on both line a and b. Static functions in C has internal linkage meaning that they are only visible in the same file. So for declaration and definition, you need to put static
so that compiler knows that the function has internal linkage. If you don't put static
on line a, you will get some compilation errors.
In the second case, since foo1
is a static function, it must be defined in the same file as main
. You only put a forward declaration without actual definition. Even though it compiles fine, the program will not link since foo1
is not defined.
Upvotes: 2