static finction

This is a discussion on static finction within the C Programming forums, part of the General Programming Boards category; what is that static used for.. and when is a good time to use it?...

  1. #1
    Registered User
    Join Date
    Dec 2001
    Posts
    227

    static finction

    what is that static used for.. and when is a good time to use it?
    Only the strong survives.

  2. #2
    Even death may die... Dante Shamest's Avatar
    Join Date
    Apr 2003
    Location
    Malaysia
    Posts
    970
    If you're talking about static member functions of classes, they are functions that do not require an object to be instantiated from the class before being used.

  3. #3
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    The static keyword can be used to limit the scope of a function or object -- to a file or a function, for example.

    It may also be used to make a block-scope object's lifetime to be the entire execution of the program -- a variable with function scope that retains its value between function calls, for example.
    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.*

  4. #4
    Registered User
    Join Date
    Dec 2001
    Posts
    227
    i didnt understand not a word you just said hehe
    anyways.. take this for example

    Code:
       static char textBuffer[1024];
            va_list args;
            va_start(args, words);
            vsprintf(textBuffer, words, args);
            va_end(args);
            return write(soc,textBuffer,strlen(textBuffer));
    its a friends code.. im just tring to understand it
    Only the strong survives.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    The function is declared as static, meaning its scope is limited to the file it is in. You cannot use this function outside of the file it is defined in, because it is static.

    If you had a variable inside a function as static, instead of, or possibly along with, the function itself, then that variable would retain whatever value it had the last time the function was called.

    Example:
    Code:
    void fun( void )
    {
        static int x;
    
        printf("x is %d\n", x );
    
        x++;
    }
    Every time you call this function, the value of x is printed, and then it is incremented. If the variable were not static, two things would happen:

    1) The value isn't ever initialized, so it would have some random value.

    2) The value would not remain retain its information for the next time the function was called.

    Static variables are automaticly initialized to zero when created, this is why we can get away without initializing it.

    Also, since it is static, it will simply increment as we indended. Meaning: The first time, it will print zero, the second time it will print one, and so on.

    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User
    Join Date
    Dec 2001
    Posts
    227
    thank you.. i get it now =) ( well better then before )
    Only the strong survives.

  7. #7
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    >You cannot use this function outside of the file it is defined in, >because it is static.

    You can't use it by name. It can still be called via a function pointer.

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, 04:06 PM
  2. seg fault at vectornew
    By tytelizgal in forum C Programming
    Replies: 2
    Last Post: 10-25-2008, 02:22 PM
  3. LNK2001 ERROR!!! need help
    By lifeafterdeath in forum C++ Programming
    Replies: 7
    Last Post: 05-27-2008, 06: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, 05:10 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21