static functions

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • prasi

    static functions

    hi, all
    I wanted to know the significance of static functions.
    can anybody explain me.
    thanks in advance
    Prasanna K P

  • pete

    #2
    Re: static functions

    prasi wrote:[color=blue]
    >
    > hi, all
    > I wanted to know the significance of static functions.
    > can anybody explain me.[/color]

    They have internal linkage.

    --
    pete

    Comment

    • Mabden

      #3
      Re: static functions

      "pete" <pfiland@mindsp ring.com> wrote in message
      news:43414412.7 553@mindspring. com...[color=blue]
      > prasi wrote:[color=green]
      > >
      > > hi, all
      > > I wanted to know the significance of static functions.
      > > can anybody explain me.[/color]
      >
      > They have internal linkage.[/color]

      They don't move...

      I guess I have been using "the new stuff" too much, but can you even
      have static ftns in C? Isn't that a C++ / C# thing?

      A function with a static variable means that the variable retains the
      value it had the last time you called it. So I can initialize a static
      variable with 0 in code, and change its value within the function - when
      you call it, and the next time you call it, it has the new value, not
      zero. If it were not a static variable, it would be zero every time.

      ex:
      x = add_one();

      int add_one(void)
      {
      static int x=0;
      return ++x;
      }

      X should increment every time you call the function, unless I did
      something wrong (which I will be raked over the coals for by random
      people with the initials ..)

      HTH,

      --
      Mabden


      Comment

      • Ulrich Eckhardt

        #4
        Re: static functions

        Mabden wrote:[color=blue]
        > I guess I have been using "the new stuff" too much, but can you even
        > have static ftns in C? Isn't that a C++ / C# thing?[/color]

        /me believes you're indeed confusing a few things. 'static' has two
        meanings in C, one being
        [color=blue]
        > A function with a static variable means that the variable retains the
        > value it had the last time you called it. So I can initialize a static
        > variable with 0 in code, and change its value within the function - when
        > you call it, and the next time you call it, it has the new value, not
        > zero. If it were not a static variable, it would be zero every time.[/color]

        ....the other meaning being when applied to otherwise global variables and
        functions, which then get internal linkage, i.e. are only visible by that
        very translation unit and don't conflict with equally named variables and
        functions in other translation units.

        Just since you mentioned other C derived languages, I know that in C++ it
        got a third meaning when used on a memberfunction or variable of a class
        or struct - in that case it means that the class rather than each instance
        of the class class has such an object, so it creates a kind of global
        object/function but with a more local scope and a few other small
        features.
        I also believe that Java uses static to create plain functions because
        otherwise it only has memberfunctions , but I'm not sure about that and
        it's not really the place to debate that...
        Other than that, using static to achieve internal linkage is deprecated
        there (should be replaced with the use of anonymous namespaces), in C it
        is still accepted and well-understood practice, so you indeed switched a
        few things around. ;)

        cheers

        Uli

        Comment

        • Mabden

          #5
          Re: static functions

          "Ulrich Eckhardt" <doomster@knuut .de> wrote in message
          news:3qd1snFe45 prU1@uni-berlin.de...[color=blue]
          > Mabden wrote:[color=green]
          > > I guess I have been using "the new stuff" too much, but can you even
          > > have static ftns in C? Isn't that a C++ / C# thing?[/color]
          >
          > /me believes you're indeed confusing a few things. 'static' has two
          > meanings in C, one being
          >[color=green]
          > > A function with a static variable means that the variable retains[/color][/color]
          the[color=blue][color=green]
          > > value it had the last time you called it. So I can initialize a[/color][/color]
          static[color=blue][color=green]
          > > variable with 0 in code, and change its value within the function -[/color][/color]
          when[color=blue][color=green]
          > > you call it, and the next time you call it, it has the new value,[/color][/color]
          not[color=blue][color=green]
          > > zero. If it were not a static variable, it would be zero every time.[/color]
          >
          > ...the other meaning being when applied to otherwise global variables[/color]
          and[color=blue]
          > functions, which then get internal linkage, i.e. are only visible by[/color]
          that[color=blue]
          > very translation unit and don't conflict with equally named variables[/color]
          and[color=blue]
          > functions in other translation units.[/color]

          That was it. I didn't want to mislead the OP but you're right a static
          function just limits it's scope to the file you are in. It is in. I case
          there were other .c files that had the same function that did something
          different. Not really something I ever saw in 20+ years of programming
          in C.

          In C# (ON topic - since mentioned in context of the OP's questions and
          other followups [btw, I love having to justify what I say to avoid
          critique - NOT!])
          .... Ahem, in C# static becomes a feature like public, and protected.
          Perhaps I should say attribute? I don't know the correct verbiage. It
          determines how an object may be used. I think it is useful for
          properties (a new thing for C, and c++ users; comes from VB) but I may
          be using the wrong terms here...

          --
          Mabden


          Comment

          • Mark B

            #6
            Re: static functions


            "Mabden" <mabden@sbc_glo bal.net> wrote in message
            news:7Yb0f.9094 $6e1.6911@newss vr14.news.prodi gy.com...[color=blue]
            > "pete" <pfiland@mindsp ring.com> wrote in message
            > news:43414412.7 553@mindspring. com...[color=green]
            >> prasi wrote:[color=darkred]
            >> >
            >> > hi, all
            >> > I wanted to know the significance of static functions.
            >> > can anybody explain me.[/color]
            >>
            >> They have internal linkage.[/color]
            >
            > They don't move...[/color]
            What?
            [color=blue]
            > I guess I have been using "the new stuff" too much,[/color]
            You're definately smoking too much of 'something'.
            [color=blue]
            > but can you even have static ftns in C?[/color]
            Yes.
            [color=blue]
            > Isn't that a C++ / C# thing?[/color]
            No.

            <snip irrelevant ramblings>[color=blue]
            > HTH,[/color]

            it didn't... although, pete's response: 'They have internal linkage'
            should have helped ;)



            Comment

            • John Bode

              #7
              Re: static functions


              prasi wrote:[color=blue]
              > hi, all
              > I wanted to know the significance of static functions.
              > can anybody explain me.
              > thanks in advance
              > Prasanna K P[/color]

              In C, a static function cannot be referenced outside of its own
              translation unit; the function name is not exported to the linker.

              For example, if I have three files, foo.c, bar.c, and main.c, and I
              have a static function foo_helper() defined in foo.c, I cannot call
              foo_helper() from any of the functions defined in bar.c or main.c.

              In a typical module, you'll have a number of support functions that
              aren't meant to be called directly from other modules, so you use the
              static keyword to make them "private" to the translation unit.

              Comment

              • Michael Wojcik

                #8
                Re: static functions


                In article <1128357626.412 902.34580@g47g2 000cwa.googlegr oups.com>, "John Bode" <john_bode@my-deja.com> writes:[color=blue]
                > prasi wrote:[color=green]
                > > I wanted to know the significance of static functions.[/color]
                >
                > In C, a static function cannot be referenced outside of its own
                > translation unit; the function name is not exported to the linker.[/color]

                Cannot be referenced *by name*. "static" applies to the name of the
                function; it doesn't do anything to the function itself. That should
                be obvious to any experienced C practitioner, but might not be to
                beginners.

                So, for example, a static function in one translation unit can be
                called from another translation unit through a function pointer,
                which might have been returned by a (non-static) function in the same
                TU as the static function, or might even be a global variable in
                the first TU:

                --- inc.c ---
                static int inc(int x) { return x+1; }
                int (*incfunc)(int) = inc;
                ---

                --- main.c ---
                #include <stdio.h>
                extern int (*incfunc)(int) ;
                int main(void)
                {
                printf("%d\n", incfunc(1));
                return 0;
                }
                ---

                This is actually useful for some abstraction purposes. For example,
                in a real inc.c, there might be multiple implementations of the "inc"
                function, all of which are hidden (via static) from other TUs. At
                program startup, incfunc(x) invokes the default implementation, but a
                (non-static) function in inc.c might be available to select a
                different one for subsequent calls. Other TUs don't need to know
                anything about this mechanism, only the API for using it.

                --
                Michael Wojcik michael.wojcik@ microfocus.com

                Even 300 years later, you should plan it in detail, when it comes to your
                summer vacation. -- Pizzicato Five

                Comment

                • John Bode

                  #9
                  Re: static functions


                  Michael Wojcik wrote:[color=blue]
                  > In article <1128357626.412 902.34580@g47g2 000cwa.googlegr oups.com>, "John Bode" <john_bode@my-deja.com> writes:[color=green]
                  > > prasi wrote:[color=darkred]
                  > > > I wanted to know the significance of static functions.[/color]
                  > >
                  > > In C, a static function cannot be referenced outside of its own
                  > > translation unit; the function name is not exported to the linker.[/color]
                  >
                  > Cannot be referenced *by name*. "static" applies to the name of the
                  > function; it doesn't do anything to the function itself. That should
                  > be obvious to any experienced C practitioner, but might not be to
                  > beginners.
                  >[/color]

                  Good catch. Thanks.

                  Comment

                  Working...