Thread: Doubt on usage of static...

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    145

    Doubt on usage of static...

    Hi All,

    1. What does following mean:
    within a file a.c ,
    static void add(); (To make following funtion private?)

    2. When do use the key word static?
    I need to use the above function in another file say b.c what will be the over functionality.In this case should i use static beside the function declaration.

    Thanks in advance
    Sanny

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    1 - Yes, it makes it local to the file it is defined in. Other functions in that file can call it, but not functions outside that file.

    2 - Whenever you feel like it. I don't use it all that often. You can also make static variables, which have a different effect.


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

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Yes, static means that it's local to the compile unit (the source itself and any included files).

    You should NOT use static if you have another compile unit (that is, generally speaking, another source file) that uses the function.

    I personally think that using static on functions is a good way to declare the intent of "it's not used anywhere else".

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    Registered User
    Join Date
    Apr 2009
    Posts
    145
    But what would be the advantage of declaring a function local to the file....

    Now what would happen :

    If i declare and define a function as
    void add() in a.c won't this as well be private to the file?
    Unless I declare the same in a a.h and include the same in b.c?

    Thanks

  5. #5
    Making mistakes
    Join Date
    Dec 2008
    Posts
    476
    Nope. In the last case, anyone could add an "extern void add(void);" definition anywhere and use that function. Static makes it local, as said, so that it can't accidentally overwrite other things, prevents namespace pollution and makes clear this function won't be used outside the file. Static functions can't be accessed from the outside.

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    If you do NOT use static, you can call add() in b.c and the compiler/linker will accept the code (it may warn you that you haven't declared the function, but it will most likely not FAIL to compile - it may not actually work, depending on what your parameters are).

    More importantly, if you have two functions, add() that are defined in a.c and b.c, the linking stage WILL complain about "duplicate symbol" or something like that. If you use the static keyword, the two functions will not be exported from the .c file, so there can be a one in each compile unit, if you like to do that [it may well be confusing].

    But imagine that you have two different modules that both have a hash-table as a storage container, one stores elements based on a string (a name) and the other on a timestamp that is two integer values: You would have a "hashIndex()" function in both files - but the hashIndex function that takes a string would be different from the one that takes a pair of integers. Now, these functions are completely meaningless outside of the module that stores stuff in the hash-table. Therefore we can make them static, and make the code look more the same, rather that making the names unique (e.g. hashIndexTimeStamp, hashIndexString).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Registered User
    Join Date
    May 2009
    Location
    Slovenia
    Posts
    5
    Hello.

    Disclaimer: I'm no expert at those things, so please be patient with me;)

    Quote Originally Posted by sanddune008 View Post
    If i declare and define a function as
    void add() in a.c won't this as well be private to the file?
    Unless I declare the same in a a.h and include the same in b.c?
    Actually, if you omit "static" in front of the function definition, compiler will export it and the linker will be able to link against it, no matter where the function has been declared (if at all).

    Let me demonstrate this with really simple application, created from two separate files.

    a.c
    Code:
    #include <stdio.h>
    
    int
    main( int    argc,
          char **argv )
    {
        /* b_print is defined in b.c */
        b_print( "Print this" );
    
        return( 0 );
    }
    b.c
    Code:
    #include <stdio.h>
    
    void
    b_print( const char *string )
    {
        printf( "OUTPUT: >> %s <<\n", string );
    }
    Now if we compile and link this application like this:
    Code:
    gcc -Wall -o b.o -c b.c
    gcc -Wall -o a.o -c a.c
    gcc -Wall -o example b.o a.o
    the second command will warn us about implicit declaration of b_print function, but other that that, application will compile and link cleanly.

    Now, if you insert "static" in front of b_print function definition and rebuild this application once again using the same commands as before, the first command will warn you about b_print function being defined but not used. Second command will produce the same warning as before. And the link command will fail, moaning about undefined reference to b_print.

    I hope this explanation makes things a bit more clear.

    BTW, I used gcc as a compiler since this is what I have at hand. Warnings may differ if you'll be using different compiler.

  8. #8
    Registered User
    Join Date
    Apr 2009
    Posts
    145
    Thanks all, for your time. Pretty much cleared my doubts

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