What is the use of static function in C?

Discussion in 'C Programming' started by vishnu, Nov 11, 2005.

  1. vishnu

    vishnu Guest

    Hi friend,
    i have a problem in my program what is the use of static function in C
    lang?
    plz help me
     
    vishnu, Nov 11, 2005
    #1
    1. Advertising

  2. vishnu

    Michael Mair Guest

    vishnu wrote:
    > i have a problem in my program what is the use of static function in C
    > lang?


    Your question is not clear.
    What is your problem? "what is the use of static function in C lang?"
    seems not to fit the description "problem in [your] program".

    If you mean
    static int foo (void);
    in contrast to
    int foo (void);
    or
    extern int foo (void);
    then look up "linkage" in your C textbook.


    Cheers
    Michael
    --
    E-Mail: Mine is an /at/ gmx /dot/ de address.
     
    Michael Mair, Nov 11, 2005
    #2
    1. Advertising

  3. vishnu

    Mike Wahler Guest

    "vishnu" <> wrote in message
    news:...
    > Hi friend,
    > i have a problem in my program


    Please describe the problem (ideally with example code
    and specific questions).

    > what is the use of static function in C
    > lang?


    Let's fix your problem first.

    -Mike
     
    Mike Wahler, Nov 11, 2005
    #3
  4. hi friend,

    what is function pointer how it is useful?
    plz help me.


    hi friend,

    what is function pointer how it is useful?
    plz help me.

    These all look like the same person to me.. I could be wrong, but is someone having wanting answer to their homework problems?


    vishnu wrote:
    > Hi friend,
    > i have a problem in my program what is the use of static function in C
    > lang?
    > plz help me
    >
     
    Dennis Willson, Nov 11, 2005
    #4
  5. vishnu

    Malcolm Guest

    "vishnu" <> wrote
    > Hi friend,
    > i have a problem in my program what is the use of static function in C
    > lang?
    > plz help me
    >

    They are hugely useful.

    Let's say I want to load a jpeg file. Bascially, all my calling programmer,
    Sheila, wants to do is pass in a filename, and get the bits back. She's a 3d
    programmer rather than a signals processing type person, and neither knows
    nor cares anything about the JPEG format.

    So she wants to call

    unsigned char *loadjpeg(char *filename, int *width, int *height)

    However JPEG files are very complicated - trying to pack the whole loader
    into a single function would make the code totally unreadable. There are
    also natural units we want to do - for instance the 8-point inverse cosine
    transform has got to be called 16 times for each block.

    Now let's say I write a function

    idct8(float *values)

    The problem is that if I make this public, someone might call it. When I go
    back to my jpeg loader, i might wnat to make things more efficient. For
    instance I might want to alter the function so that it doesn't use floating
    point.
    The other problem is that Fred, who is doing audio, might also need a idct8
    function. In his case the 8 means that it is dealing with 8-bit samples.
    So by just declaring the loadjpeg function with external linkage, and making
    everything else static, I protect my code, don't pollute namespace, and make
    it clear to Sheila that I am contracting to load a jpeg and return the bits,
    but I don't provide any math routines or anything else.
     
    Malcolm, Nov 12, 2005
    #5
  6. vishnu

    Guest

    Malcolm wrote:

    > The other problem is that Fred, who is doing audio, might also need a idct8
    > function. In his case the 8 means that it is dealing with 8-bit samples.
    > So by just declaring the loadjpeg function with external linkage, and making
    > everything else static, I protect my code, don't pollute namespace, and make
    > it clear to Sheila that I am contracting to load a jpeg and return the bits,
    > but I don't provide any math routines or anything else.


    Another benefit of static is to give additional freedom to compiler to
    do optimizations. The compiler knows the function is local to this
    translation
    unit and can do things like inlining. I remember reading someone
    commenting
    that it would have been far better for functions to default to static
    rather than
    global (external linkage). But I guess its too late to change that now
    and it is always
    better to write new functions as static if possible.

    Karthik
     
    , Nov 13, 2005
    #6
  7. vishnu

    Malcolm Guest

    <> wrote
    >
    >> The other problem is that Fred, who is doing audio, might also need a
    >> idct8
    >> function. In his case the 8 means that it is dealing with 8-bit samples.
    >> So by just declaring the loadjpeg function with external linkage, and
    >> making
    >> everything else static, I protect my code, don't pollute namespace, and
    >> make
    >> it clear to Sheila that I am contracting to load a jpeg and return the
    >> bits,
    >> but I don't provide any math routines or anything else.

    >
    > Another benefit of static is to give additional freedom to compiler to
    > do optimizations. The compiler knows the function is local to this
    > translation unit and can do things like inlining. I remember reading
    > someone
    > commenting that it would have been far better for functions to default to
    > static
    > rather than global (external linkage). But I guess its too late to change
    > that now
    > and it is always better to write new functions as static if possible.
    >

    That is probably a good snapshot of current compiler practise, but
    technology changes.
    There is no reason a compiler running on a modern PC cannot load the whole
    of a largeish program into memory in C source form, and do a full compile,
    within a few seconds. In the days when 66Mhz was leading edge, of course you
    needed the object file / linker workaround to make compile times acceptable.
    (It was the same story with "register". Nowadays it is unusual for the
    programmer to be able to make better decisions than the compiler.)

    The other point is that runtime efficiency isn't as important as it was.
    Most computers nowadays are fast enough, and a good program is one that does
    what it says and is easy to use, not one that responds quickly.

    Having said that, I have just entered the brave world of Beowulf cluster
    massively (well, 40 nodes) parallel supercomputing, and I am resorting to
    Fortran to speed things up a bit.
     
    Malcolm, Nov 13, 2005
    #7
  8. vishnu

    Flash Gordon Guest

    Malcolm wrote:
    > <> wrote
    >>> The other problem is that Fred, who is doing audio, might also need a
    >>> idct8
    >>> function. In his case the 8 means that it is dealing with 8-bit samples.
    >>> So by just declaring the loadjpeg function with external linkage, and
    >>> making
    >>> everything else static, I protect my code, don't pollute namespace, and
    >>> make
    >>> it clear to Sheila that I am contracting to load a jpeg and return the
    >>> bits,
    >>> but I don't provide any math routines or anything else.

    >> Another benefit of static is to give additional freedom to compiler to
    >> do optimizations. The compiler knows the function is local to this
    >> translation unit and can do things like inlining. I remember reading
    >> someone
    >> commenting that it would have been far better for functions to default to
    >> static
    >> rather than global (external linkage). But I guess its too late to change
    >> that now
    >> and it is always better to write new functions as static if possible.
    >>

    > That is probably a good snapshot of current compiler practise, but
    > technology changes.


    The reason it is best to write functions as static if possible is not
    only because of the compiler, it is also because of the human reader. If
    a function is static them you know it is only used locally (unless its
    address is taken) which limits how much you have to look at when trying
    to fix a bug in it. Otherwise you have to check where else it might be
    called from just in case something relies on the behaviour you are about
    to change.

    > There is no reason a compiler running on a modern PC cannot load the whole
    > of a largeish program into memory in C source form, and do a full compile,
    > within a few seconds.


    That depends on how large the source base is. Loading the source base of
    openoffice in to memory and compiling it will take more than a few
    seconds on a modern PC.

    > In the days when 66Mhz was leading edge, of course you
    > needed the object file / linker workaround to make compile times acceptable.


    If you think that is no longer required now you don't work on large
    projects. Try looking up the build time fir a Gentoo Linux system where
    everything is built from source.

    > (It was the same story with "register". Nowadays it is unusual for the
    > programmer to be able to make better decisions than the compiler.)


    That is true.

    > The other point is that runtime efficiency isn't as important as it was.


    That is the attitude that forces people to upgrade hardware. It is also
    not true in embedded systems where you might have a limit to the
    processing power you can use because of a limit on the amount of heat
    you are allowed to dissipate (this applies to at least some avionics
    systems).

    > Most computers nowadays are fast enough, and a good program is one that does
    > what it says and is easy to use, not one that responds quickly.


    A good program is one that does what is required correctly within an
    acceptable amount of time and cost an acceptable amount to develop.
    These are competing requirements but some things, such as not making the
    optimisers job harder than necessary (e.g. declaring functions as
    static when they are not called directly from outside the translation
    unit) cost very little or nothing but can help meet the performance
    requirements.

    > Having said that, I have just entered the brave world of Beowulf cluster
    > massively (well, 40 nodes) parallel supercomputing, and I am resorting to
    > Fortran to speed things up a bit.


    Performance will probably always be an issue because people will always
    want the software to do more. So you should always make it easy for the
    optimiser and maintainer by them the information you have, such as that
    a function is only called from within the one translation unit.
    --
    Flash Gordon
    Living in interesting times.
    Although my email address says spam, it is real and I read it.
     
    Flash Gordon, Nov 13, 2005
    #8
  9. vishnu

    Malcolm Guest

    "Flash Gordon" <> wrote
    >
    >> That is probably a good snapshot of current compiler practise, but
    >> technology changes.

    >
    > The reason it is best to write functions as static if possible is not only
    > because of the compiler, it is also because of the human reader. If a
    > function is static them you know it is only used locally (unless its
    > address is taken) which limits how much you have to look at when trying to
    > fix a bug in it. Otherwise you have to check where else it might be called
    > from just in case something relies on the behaviour you are about to
    > change.
    >

    That's the real answer. Usually the priority is to have clean,
    understandable code. Speed of execution / memory usage / compiling problems
    are almost always secondary, except in the inner loops of time-critical
    programs.
    >
    > That depends on how large the source base is. Loading the source base of
    > openoffice in to memory and compiling it will take more than a few seconds
    > on a modern PC.
    >

    I got a new PC a few weeks ago, and it has 2 GB of memory installed.
    1MB of ASCII represents about 100,000 words of English, or an average novel.
    That's probably a good year's work for a programmer.
    So 2 Gigabytes holds 2000 programmer year's work. That's a large project,
    way beyond the resources of all but the largest companies.

    >
    > If you think that is no longer required now you don't work on large
    > projects. Try looking up the build time fir a Gentoo Linux system where
    > everything is built from source.
    >

    The reason is that the compiler is designed to treat core as a limited
    resource. So it loads each file individually, outputs an object file, then
    loads all the object files back in to link them.

    There's no reason for a modern compiler to work like that - it can hold all
    the source in core for all bu the largest projects. True, you cannot compile
    a project with over 2GB of source on an average PC, but given that the 2000
    programmer years will cost over a hundred million dollars, a nice
    supercomputer hosting a cross compiler won't break the budget.

    > Performance will probably always be an issue because people will always
    > want the software to do more. So you should always make it easy for the

    ^^^^^
    > optimiser and maintainer by them the information you have, such as that a
    > function is only called from within the one translation unit.
    >

    Usually you are right - there's no point throwing cycles away. However you
    cannot declare static functions in Fortran 77, which is the native language
    of the Beowulf cluster. So to make all code follow the same conventions is
    maybe a good idea - just maybe.
     
    Malcolm, Nov 15, 2005
    #9
  10. vishnu

    Guest

    vishnu schrieb:

    > Hi friend,
    > i have a problem in my program what is the use of static function in C
    > lang?
    > plz help me


    You must avoid using static functions at all costs if there are dynamic
    functions available! Use dynamic functions whenever possible.
     
    , Nov 16, 2005
    #10
  11. vishnu

    pete Guest

    wrote:
    >
    > vishnu schrieb:
    >
    > > Hi friend,
    > > i have a problem in my program what is the use of
    > > static function in C lang?
    > > plz help me

    >
    > You must avoid using static functions
    > at all costs if there are dynamic
    > functions available! Use dynamic functions whenever possible.


    static functions are a feature of standard C.
    "dynamic functions" means nothing in the context of standard C.

    --
    pete
     
    pete, Nov 16, 2005
    #11
  12. vishnu

    Chris Hills Guest

    In article <dl72j5$60v$-infra.bt.com>, Malcolm
    <> writes
    >


    >The other point is that runtime efficiency isn't as important as it was.


    Absolutely un-true!

    >Most computers nowadays are fast enough, and a good program is one that does
    >what it says and is easy to use, not one that responds quickly.


    Most computers on the planet are 8 bit micros with 256 RAM and 64K code
    space..... At one time there were 3 or 4 in every PC.

    In embedded systems (ie vitally *ANYTHING* that has electrical power
    these days) still work on the smaller the memory the better. The cost of
    adding a 1 dollar memory chip on to a system can cost 2000 USD in
    engineering time and 100,000 USD a year to a project producing 50K units

    In a car if you added an additional memory chip to each of the 100 MCU
    in the system you will be adding 20,000 USD to the NRE of the car. Then
    the productions costs would go up by 200 USD per car...

    (all figures for illustration only and are approximate based on 20 years
    experience)


    --
    \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
    \/\/\/\/\ Chris Hills Staffs England /\/\/\/\/
    /\/\/ www.phaedsys.org \/\/\
    \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/
     
    Chris Hills, Nov 16, 2005
    #12
  13. vishnu

    Guest

    pete schrieb:

    > wrote:
    > >
    > > vishnu schrieb:
    > >
    > > > Hi friend,
    > > > i have a problem in my program what is the use of
    > > > static function in C lang?
    > > > plz help me

    > >
    > > You must avoid using static functions
    > > at all costs if there are dynamic
    > > functions available! Use dynamic functions whenever possible.

    >
    > static functions are a feature of standard C.
    > "dynamic functions" means nothing in the context of standard C.


    Did I say it meant anything?
     
    , Nov 16, 2005
    #13
  14. On 2005-11-16 12:19:20 -0500, said:

    >
    > pete schrieb:
    >
    >> wrote:
    >>>
    >>> vishnu schrieb:
    >>>
    >>>> Hi friend,
    >>>> i have a problem in my program what is the use of
    >>>> static function in C lang?
    >>>> plz help me
    >>>
    >>> You must avoid using static functions
    >>> at all costs if there are dynamic
    >>> functions available! Use dynamic functions whenever possible.

    >>
    >> static functions are a feature of standard C.
    >> "dynamic functions" means nothing in the context of standard C.

    >
    > Did I say it meant anything?


    Well, by using it, you certainly implied that it meant something.
    Unless, that is, you're in the habit of using meaningless terms.

    Perhaps you should clarify what it is yea meant when you said "dynamic
    functions".

    --
    Clark S. Cox, III
     
    Clark S. Cox III, Nov 16, 2005
    #14
  15. pete <> writes:
    > wrote:
    >> vishnu schrieb:
    >>
    >> > Hi friend,
    >> > i have a problem in my program what is the use of
    >> > static function in C lang?
    >> > plz help me

    >>
    >> You must avoid using static functions
    >> at all costs if there are dynamic
    >> functions available! Use dynamic functions whenever possible.

    >
    > static functions are a feature of standard C.
    > "dynamic functions" means nothing in the context of standard C.


    I think quetzalcotl was trying to provide an appropriate level of help
    to someone asking about a homework problem.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
     
    Keith Thompson, Nov 16, 2005
    #15
  16. a écrit :
    > Did I say it meant anything?


    ** Troll Alert ***

    --
    A+

    Emmanuel Delahaye
     
    Emmanuel Delahaye, Nov 16, 2005
    #16
  17. On Wed, 16 Nov 2005 14:22:43 +0000, in comp.lang.c , Chris Hills
    <> wrote:

    >In article <dl72j5$60v$-infra.bt.com>, Malcolm
    ><> writes
    >>

    >
    >>The other point is that runtime efficiency isn't as important as it was.

    >
    >Absolutely un-true!


    Even setting aside the microcontroller aspect, this is dangerous, and
    I sincerely hope that CS schools don't teach it.

    I have a serious problem at work with programmers assuming unlimited
    resources - about 2% of our overnight processing now consumes more
    than 4GB of memory. If you've ever tried sourcing 16GB of memory for a
    new Sun server, you'll know that this is an expensive mistake...

    --
    Mark McIntyre
    CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
    CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>

    ----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
    http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
    ----= East and West-Coast Server Farms - Total Privacy via Encryption =----
     
    Mark McIntyre, Nov 16, 2005
    #17
  18. vishnu

    Guest

    Clark S. Cox III schrieb:

    > On 2005-11-16 12:19:20 -0500, said:
    >
    > >
    > > pete schrieb:
    > >
    > >> wrote:
    > >>>
    > >>> vishnu schrieb:
    > >>>
    > >>>> Hi friend,
    > >>>> i have a problem in my program what is the use of
    > >>>> static function in C lang?
    > >>>> plz help me
    > >>>
    > >>> You must avoid using static functions
    > >>> at all costs if there are dynamic
    > >>> functions available! Use dynamic functions whenever possible.
    > >>
    > >> static functions are a feature of standard C.
    > >> "dynamic functions" means nothing in the context of standard C.

    > >
    > > Did I say it meant anything?

    >
    > Well, by using it, you certainly implied that it meant something.
    > Unless, that is, you're in the habit of using meaningless terms.


    I'm in the habit of giving appropriate answers to stupid questions. It
    is obvious to me that the OP knows near to nothing about functions in C
    or about the effect of the keyword static. In other words, he knows
    almost nothing. If he knew about those topics, he could easily infer
    what the difference between

    int f() { .. }

    and

    static int f() { ... }

    is. It would be another case, if he asked like so: "I know, that static
    items (not just functions) cannot be seen from other compilation units.
    I just can't imagine how this is useful. Please enlighten me."

    Another, albeit less irritating appropriate answer could have been:
    "Well, in

    static int f(int a, int b) { return a+b; }

    I define a static function that computes the sum of two integers, a and
    b. This static function can then be used only for that purpose. If, for
    example, you want to use a static function to add two doubles, x and y,
    you cannot use f. Fortunately, it is possible in most cases to define
    another static function that does just what you want to use it for."
     
    , Nov 17, 2005
    #18
  19. vishnu

    Chris Hills Guest

    In article <>,
    writes
    >
    >Clark S. Cox III schrieb:
    >
    >> On 2005-11-16 12:19:20 -0500, said:
    >>
    >> >
    >> > pete schrieb:
    >> >
    >> >> wrote:
    >> >>>
    >> >>> vishnu schrieb:
    >> >>>
    >> >>>> Hi friend,
    >> >>>> i have a problem in my program what is the use of
    >> >>>> static function in C lang?
    >> >>>> plz help me
    >> >>>
    >> >>> You must avoid using static functions
    >> >>> at all costs if there are dynamic
    >> >>> functions available! Use dynamic functions whenever possible.
    >> >>
    >> >> static functions are a feature of standard C.
    >> >> "dynamic functions" means nothing in the context of standard C.
    >> >
    >> > Did I say it meant anything?

    >>
    >> Well, by using it, you certainly implied that it meant something.
    >> Unless, that is, you're in the habit of using meaningless terms.

    >
    >I'm in the habit of giving appropriate answers to stupid questions. It
    >is obvious to me that the OP knows near to nothing about functions in C
    >or about the effect of the keyword static. In other words, he knows
    >almost nothing. If he knew about those topics,



    If he knew he would not be asking. static functions and static variables
    are often confused by students and new programmers (and many old ones
    too :)

    I have noticed that there is a far more vindictive streak in this NG
    these days compared to when I started reading it 15 off years ago.

    Whilst no one wants to do home work for some one you can always point
    them in the right direction of study.

    When people come here to ask "the experts" they don't expect them to
    behave like self righteous pompous twats.





    --
    \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
    \/\/\/\/\ Chris Hills Staffs England /\/\/\/\/
    /\/\/ www.phaedsys.org \/\/\
    \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/
     
    Chris Hills, Nov 17, 2005
    #19
  20. Chris Hills wrote:
    > In article <>,
    > >> >>> vishnu schrieb:
    > >> >>>
    > >> >>>> Hi friend,
    > >> >>>> i have a problem in my program what is the use of
    > >> >>>> static function in C lang?
    > >> >>>> plz help me
    > >> >>>


    >
    > I have noticed that there is a far more vindictive streak in this NG
    > these days compared to when I started reading it 15 off years ago.
    >
    > Whilst no one wants to do home work for some one you can always point
    > them in the right direction of study.
    >
    > When people come here to ask "the experts" they don't expect them to
    > behave like self righteous pompous twats.
    >


    <OT> Yes. Though vague questions much like above, written in exactly
    that
    style and wording, sometimes seem to be trolls, perhaps by a
    single
    person (chellapa?). Mostly coming from gmail accounts over
    google
    groups (yes, I am too)

    Some recent examples
    >>sabarish:
    >>Hi friend,
    >>what is the use of function pointer in c language and where it is
    >>useful?
    >>tell with simple example...?
    >>plz help me.


    >>jkwagh
    >>Hi friend,
    >>I am currently working on a Parser project in which i want to parse
    >>HTML and asp pages into .c file. So can you plz help me to do that?
    >>Thanks in advanced
    >>Jayant


    >>chellapa
    >>hi all
    >>I need to write a program to find mac address of a my computer using
    >>libaries, is
    >>this possible? How?
    >>thanks


    I don't think they are worth a real answer -- they
    indicate a complete lack of respect for the time of those
    being
    questioned in that the poster has put no effort into the
    matter
    themselves and has not tried to find the right place to ask
    the question. While rude/cruel responses aren't called for,
    neither
    are thoughtful answer to thoughtless questions. Perhaps the
    correct answer to these posts is just a url:
    http://www.catb.org/~esr/faqs/smart-questions.html
    </OT>

    -David
     
    David Resnick, Nov 17, 2005
    #20
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Victor Bazarov
    Replies:
    0
    Views:
    1,367
    Victor Bazarov
    Jun 23, 2003
  2. John Black
    Replies:
    1
    Views:
    390
    Daniel T.
    May 31, 2004
  3. Replies:
    4
    Views:
    1,082
    Richard Tobin
    Dec 12, 2006
  4. Assertor
    Replies:
    1
    Views:
    607
    Victor Bazarov
    Mar 9, 2006
  5. Replies:
    2
    Views:
    521
    Jonathan Mcdougall
    Aug 6, 2007
Loading...

Share This Page