Static functions in C
The static keyword with a function in C makes it Static function. The function without static keyword in C are global by default. The difference between them is static function are only accessible in the file in which it is defined. Static function are used so that access to these function can be restricted as well as making functions static makes function name reusable in other files.
Example of Static function in C:
Below function is defined in file1.c:
static int fun(void)
{
printf("static function ");
}
Below function is defined in file2.c:
int main(void)
{
fun();
getchar();
return 0;
}
Compiling above code with command “gcc file2.c file1.c”, the error obtained is undefined reference to fun . This is because fun() is declared static in file1.c and cannot be used in file2.c.
0 Comment(s)