In C, functions are global by default. The “static” keyword before a function name makes it static.
For example, the below function fun() is static.
C
static int fun( void ) {
printf ( "I am a static function " );
}
|
Unlike global functions in C, access to static functions is restricted to the file where they are declared. Therefore, when we want to restrict access to functions, we make them static. Another reason for making functions static can be the reuse of the same function name in other files.
For example, if we store the following program in one file file1.c
C
static void fun1( void ) {
puts ( "fun1 called" );
}
|
And store the following program in another file file2.c
C
int main( void )
{
fun1();
getchar ();
return 0;
}
|
Now, if we compile the above code with the command “gcc file2.c file1.c”, we get the error “undefined reference to `fun1’”. This is because fun1() is declared static in file1.c and cannot be used in file2.c.
Related Articles:
Please write comments if you find anything incorrect in the above article, or want to share more information about static functions in C.
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!