WOuld like to know what is the point of declaring static functions?
WOuld like to know what is the point of declaring static functions?
Only by the cross are you saved...
Functions declared as static have file scope. That is, they may only be called in the module they are defined in. This is useful if you want to have 'private' functions. An example might be something like this.Code:/* myobj.h */ void MyObjInit(void); /* 'public' function */ void MyObjOpen(void); /* 'public' function */ void MyObjClose(void); /* 'public' function */Code:/* myobj.c */ static void MyObjIoctl(int i) /* 'private' function */ { /* ... */ } void MyObjInit(void) /* 'public' function */ { MyObjIoctl(0); /* ... */ } void MyObjOpen(void) /* 'public' function */ { MyObjIoctl(1); /* ... */ } void MyObjClose(void) /* 'public' function */ { MyObjIoctl(2); /* ... */ }Code:/* main.c */ #include "myobj.h" int main(void) { /* Can't do this: MyObjIoctl(0); */ MyObjInit(); MyObjOpen(); /* ... */ MyObjClose(); return 0; }
7. It is easier to write an incorrect program than understand a correct one.
40. There are two ways to write error-free programs; only the third one works.*
Generally, only functions that will not be placed in a header file should be declared static.
In fact, when creating reusable program modules, you should declare all variables in the module as static and put only functions in the header files to set these variables.
Peter O. Programming tips
If you declare an array and tried to send the address of the array to the calling function, the program after function call removes the stack frame allocated for that function call. So you will not get the expected result. In order to avoid this you can declare that array inside the function as static array.
Lot there to say about static. Above is one sample.
Saravanan.
Saravanan.T.S.
Beginner.