Thread: static

  1. #1
    Registered User
    Join Date
    Jun 2003
    Posts
    147

    static

    WOuld like to know what is the point of declaring static functions?
    Only by the cross are you saved...

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    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.*

  3. #3
    Registered User
    Join Date
    Feb 2003
    Posts
    76
    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.

  4. #4
    Registered User
    Join Date
    Jul 2003
    Posts
    102

    Hi

    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  2. seg fault at vectornew
    By tytelizgal in forum C Programming
    Replies: 2
    Last Post: 10-25-2008, 01:22 PM
  3. LNK2001 ERROR!!! need help
    By lifeafterdeath in forum C++ Programming
    Replies: 7
    Last Post: 05-27-2008, 05:05 PM
  4. get keyboard and mouse events
    By ratte in forum Linux Programming
    Replies: 10
    Last Post: 11-17-2007, 05:42 PM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM