What is the point of making a function static in C?
-
8@nightcracker: There are no such things as "methods" in C++. I think you're confused with Objective-C.– Bo PerssonMar 16, 2011 at 16:17
-
1Nah, I'm confused with Python. A function inside a class is called a method in Python.– orlpMar 16, 2011 at 16:25
-
7possible duplicate of What is a "static" function? (in C)– atoMerzMar 7, 2014 at 11:53
7 Answers
Making a function static hides it from other translation units, which helps provide encapsulation.
helper_file.c
int f1(int); /* prototype */
static int f2(int); /* prototype */
int f1(int foo) {
return f2(foo); /* ok, f2 is in the same translation unit */
/* (basically same .c file) as f1 */
}
int f2(int foo) {
return 42 + foo;
}
main.c:
int f1(int); /* prototype */
int f2(int); /* prototype */
int main(void) {
f1(10); /* ok, f1 is visible to the linker */
f2(12); /* nope, f2 is not visible to the linker */
return 0;
}
-
10Is translation unit the correct terminology to use here? Wouldn't object file be more accurate? From what I understand, a static function is hidden from the linker and the linker does not operate on translation units. Feb 13, 2014 at 18:32
-
3I should have also said, that I like to think of it as being hidden from the linker; it seems clearer that way. Feb 13, 2014 at 18:51
-
1so, internal function (that we sure not to call it outside of its c file), we should put it as static function, right ? So, we can sure it cannot call elsewhere. Thanks :)– hqtJul 6, 2014 at 3:30
-
2How do you compile this? Do you use
#include <helper_file.c>? I think that would make it a single translation unit then...– AtcoldAug 23, 2016 at 1:30 -
2@Atcold: the way I wrote the code you simply include the 2 source files in the command line, like this
gcc -std=c99 -pedantic -Wall -Wextra main.c helper_file.c. The prototypes for the functions are present in both source files (no need for header files). The linker will resolve the functions.– pmgAug 23, 2016 at 8:37
pmg is spot on about encapsulation; beyond hiding the function from other translation units (or rather, because of it), making functions static can also confer performance benefits in the presence of compiler optimizations.
Because a static function cannot be called from anywhere outside of the current translation unit (unless the code takes a pointer to its address), the compiler controls all the call points into it.
This means that it is free to use a non-standard ABI, inline it entirely, or perform any number of other optimizations that might not be possible for a function with external linkage.
-
12
-
1@caf What do you mean by function's address is taken? To me, the notion of functions/variable having addresses or being assigned address at compile time is a little confusing. Can you please elaborate? Aug 2, 2013 at 12:43
-
2@crypticcoder: Your program is loaded in memory, therefore functions also have a memory location and the address can be obtained. With a function pointer, you could call any of those. If you do that, it reduces the list of optimizations the compiler can perform since the code must stay intact at the same place.– user1454661Aug 2, 2013 at 17:23
-
5@crypticcoder: I mean that an expression evaluates a pointer to the function and does something with it other than immediately call the function. If a pointer to a
staticfunction escapes the current translation unit, then that function could be directly called from other translation units.– cafAug 3, 2013 at 0:02 -
1@caf if the function's address is taken, would the compiler detect that and turn off the static function optimizations mentioned in this answer (eg using a non-standard ABI)? I suppose it'd have to.– sevkoJul 12, 2018 at 15:52
The static keyword in C is used in a compiled file (.c as opposed to .h) so that the function exists only in that file.
Normally, when you create a function, the compiler generates cruft the linker can use to, well, link a function call to that function. If you use the static keyword, other functions within the same file can call this function (because it can be done without resorting to the linker), while the linker has no information letting other files access the function.
-
23Doub: Use of the word "cruft" is more precise than you give it credit for. In the context of the question, "cruft" is the right word to use here. Nov 20, 2014 at 21:16
-
1@3Doubloons I agree that it's simplified, but I think that makes it that much easier to understand for beginners. Mar 25, 2015 at 7:47
Looking at the posts above I would like to give a more clarified answer:
Suppose our main.c file looks like this:
#include "header.h"
int main(void) {
FunctionInHeader();
}
Now consider three cases:
Case 1: Our
header.hfile looks like this:#include <stdio.h> static void FunctionInHeader(); void FunctionInHeader() { printf("Calling function inside header\n"); }Then the following command on linux:
gcc main.c -o mainwill succeed! That's because after the
main.cfile includes theheader.h, the static function definition will be in the samemain.cfile (more precisely, in the same translation unit) to where it's called.If one runs
./main, the output will beCalling function inside header, which is what that static function should print.Case 2: Our header
header.hlooks like this:static void FunctionInHeader();and we also have one more file
header.c, which looks like this:#include <stdio.h> #include "header.h" void FunctionInHeader() { printf("Calling function inside header\n"); }Then the following command
gcc main.c header.c -o mainwill give an error. In this case
main.cincludes only the declaration of the static function, but the definition is left in another translation unit and thestatickeyword prevents the code defining a function to be linkedCase 3:
Similar to case 2, except that now our header
header.hfile is:void FunctionInHeader(); // keyword static removedThen the same command as in case 2 will succeed, and further executing
./mainwill give the expected result. Here theFunctionInHeaderdefinition is in another translation unit, but the code defining it can be linked.
Thus, to conclude:
static keyword prevents the code defining a function to be linked,
when that function is defined in another translation unit than where it is called.
C programmers use the static attribute to hide variable and function declarations inside modules, much as you would use public and private declarations in Java and C++. C source files play the role of modules. Any global variable or function declared with the static attribute is private to that module. Similarly, any global variable or function declared without the static attribute is public and can be accessed by any other module. It is good programming practice to protect your variables and functions with the static attribute wherever possible.
pmg's answer is very convincing. If you would like to know how static declarations work at object level then this below info could be interesting to you. I reused the same program written by pmg and compiler it into a .so(shared object) file
Following contents are after dumping the .so file into something human readable
0000000000000675 f1: address of f1 function
000000000000068c f2: address of f2(staticc) function
note the difference in the function address , it means something . For a function that's declared with different address , it can very well signify that f2 lives very far away or in a different segment of the object file.
Linkers use something called PLT(Procedure linkage table) and GOT(Global offsets table) to understand symbols that they have access to link to .
For now think that GOT and PLT magically bind all the addresses and a dynamic section holds information of all these functions that are visible by linker.
After dumping the dynamic section of the .so file we get a bunch of entries but only interested in f1 and f2 function.
The dynamic section holds entry only for f1 function at address 0000000000000675 and not for f2 !
Num: Value Size Type Bind Vis Ndx Name
9: 0000000000000675 23 FUNC GLOBAL DEFAULT 11 f1
And thats it !. From this its clear that the linker will be unsuccessful in finding the f2 function since its not in the dynamic section of the .so file.
When there is a need to restrict access to some functions, we'll use the static keyword while defining and declaring a function.
/* file ab.c */
static void function1(void)
{
puts("function1 called");
}
And store the following code in another file ab1.c
/* file ab1.c */
int main(void)
{
function1();
getchar();
return 0;
}
/* in this code, we'll get a "Undefined reference to function1".Because function 1 is declared static in file ab.c and can't be used in ab1.c */
-
1