Why people love to ask about static functions is no idea. But the fact is that static functions are just an extra-ordinary functions compared to normal functions.
Gotchas while using Static functions.
1) Static functions are limited to file where they are declared.
2) You can define the same name of the function in another file.
3) Global functions have global scope, meaning function defined in one file can be accessed by other module.
Example for static function
File1.c
static void fun1()
{
printf("I am a static function") ;
return ;
}
The keyword static makes the function as static and the call will be used only within File1.c file.
Another example
File2.c
static void Func2()
{
printf("Another static demo\n") ;
return ;
}
File3.c
void Func2()
{
printf("This is normal function") ;
return ;
}
main.c
int main()
{
Func2() ;
Func2() ;
}
The fuction Func2() is defined twice. If static function was used you will get an error. Both File2.o and File3.o can used to create an executale main.o(main.c).
Gotchas while using Static functions.
1) Static functions are limited to file where they are declared.
2) You can define the same name of the function in another file.
3) Global functions have global scope, meaning function defined in one file can be accessed by other module.
Example for static function
File1.c
static void fun1()
{
printf("I am a static function") ;
return ;
}
The keyword static makes the function as static and the call will be used only within File1.c file.
Another example
File2.c
static void Func2()
{
printf("Another static demo\n") ;
return ;
}
File3.c
void Func2()
{
printf("This is normal function") ;
return ;
}
main.c
int main()
{
Func2() ;
Func2() ;
}
The fuction Func2() is defined twice. If static function was used you will get an error. Both File2.o and File3.o can used to create an executale main.o(main.c).
No comments:
Post a Comment