Static function prototype

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

    Static function prototype

    I seem to remember that in ANSI C, all static functions should have
    their function prototypes listed at the beginning of the file for
    better consistency (or something like that). I googled and didn't find
    any good explanation about it. Could the experts please give me an
    explanation or point me to some link? Thanks!

    /Why Tea
  • danmath06@gmail.com

    #2
    Re: Static function prototype

    On Oct 3, 2:08 pm, Why Tea <ytl...@gmail.c omwrote:
    I seem to remember that in ANSI C, all static functions should have
    their function prototypes listed at the beginning of the file for
    better consistency (or something like that). I googled and didn't find
    any good explanation about it. Could the experts please give me an
    explanation or point me to some link? Thanks!
    >
    /Why Tea
    In a file each function can only see the ones declared above it.
    That's why it's good practice to have all the prototypes at the top of
    the file. The functions that have not been declared before you invoke
    them will be asumed to return int and as far as I know the arguments
    will not be checked because there is no definition to check them
    against to. This applies to all functions static or not.


    Comment

    • Eric Sosman

      #3
      Re: Static function prototype

      Why Tea wrote:
      I seem to remember that in ANSI C, all static functions should have
      their function prototypes listed at the beginning of the file for
      better consistency (or something like that). I googled and didn't find
      any good explanation about it. Could the experts please give me an
      explanation or point me to some link? Thanks!
      Stepping back a little, it's always been good practice to
      declare functions, static or otherwise, before using them. (C99
      makes this not just good practice, but a requirement.) And ever
      since ANSI C appeared, it's been good practice to use prototypes
      in function declarations and definitions. So the advice about
      declaring functions applies to all functions, not just to static
      functions.

      Now to matters of layout, which is what your question really
      boils down to. Non-static functions in "serious" programs should
      usually be declared by including the associated headers, so all
      you see in the .c source file is usually a few #include directives.
      Static functions should almost never be declared in headers: You
      made them static because you wanted their names to be visible only
      inside one module, so why would you export those names to other
      modules? Thus, static functions are usually declared differently
      from external functions -- the declarations themselves look the
      same, apart from `static', but their placement is different.

      Two principal styles of arranging functions within a module
      are commonly seen. One puts the top-level functions -- main(),
      for example -- early in the file, with subordinate functions later
      and low-level "leaf" functions last of all. In this style the
      function's definition usually appears after at least one of its
      calls, perhaps after all of them, so good practice (or C99's
      mandate) calls for putting a separate declaration somewhere prior
      to the first use. The top of the file is a pretty good location.

      Some people (I'm one of them) dislike telling the compiler the
      same thing twice; it irks us to declare that foo() takes two int
      arguments and returns a double, and then turn right around and
      write the actual definition of foo() with two int arguments and a
      double result. When we get irked enough, we sometimes take advantage
      of the fact that a function's definition can also serve as its
      declaration: If the definition appears before the function's first
      use, no separate declaration is needed. So we write the module
      "upside-down" or "Pascal-style:" low-level "leaf" functions at the
      top, followed by intermediate-level functions, and the Big Kahuna
      last of all, each function declared by its own definition.

      If you've got two or more mutually recursive functions -- f()
      calls g() *and* g() calls f() -- you won't be able to use the
      definitions-are-declarations strategy for all of them. If f()
      appears first, its call to g() appears before the definition of g()
      and you'll need to insert a separate declaration of g() somewhere
      before f() in the file.

      --
      Eric.Sosman@sun .com

      Comment

      • Why Tea

        #4
        Re: Static function prototype

        On Oct 3, 11:59 am, Eric Sosman <Eric.Sos...@su n.comwrote:
        Why Tea wrote:
        I seem to remember that in ANSI C, all static functions should have
        their function prototypes listed at the beginning of the file for
        better consistency (or something like that). I googled and didn't find
        any good explanation about it. Could the experts please give me an
        explanation or point me to some link? Thanks!
        >
             Stepping back a little, it's always been good practice to
        declare functions, static or otherwise, before using them.  (C99
        makes this not just good practice, but a requirement.)  And ever
        since ANSI C appeared, it's been good practice to use prototypes
        in function declarations and definitions.  So the advice about
        declaring functions applies to all functions, not just to static
        functions.
        >
             Now to matters of layout, which is what your question really
        boils down to.  Non-static functions in "serious" programs should
        usually be declared by including the associated headers, so all
        you see in the .c source file is usually a few #include directives.
        Static functions should almost never be declared in headers: You
        made them static because you wanted their names to be visible only
        inside one module, so why would you export those names to other
        modules?  Thus, static functions are usually declared differently
        from external functions -- the declarations themselves look the
        same, apart from `static', but their placement is different.
        >
             Two principal styles of arranging functions within a module
        are commonly seen.  One puts the top-level functions -- main(),
        for example -- early in the file, with subordinate functions later
        and low-level "leaf" functions last of all.  In this style the
        function's definition usually appears after at least one of its
        calls, perhaps after all of them, so good practice (or C99's
        mandate) calls for putting a separate declaration somewhere prior
        to the first use.  The top of the file is a pretty good location.
        >
             Some people (I'm one of them) dislike telling the compiler the
        same thing twice; it irks us to declare that foo() takes two int
        arguments and returns a double, and then turn right around and
        write the actual definition of foo() with two int arguments and a
        double result.  When we get irked enough, we sometimes take advantage
        of the fact that a function's definition can also serve as its
        declaration: If the definition appears before the function's first
        use, no separate declaration is needed.  So we write the module
        "upside-down" or "Pascal-style:" low-level "leaf" functions at the
        top, followed by intermediate-level functions, and the Big Kahuna
        last of all, each function declared by its own definition.
        >
             If you've got two or more mutually recursive functions -- f()
        calls g() *and* g() calls f() -- you won't be able to use the
        definitions-are-declarations strategy for all of them.  If f()
        appears first, its call to g() appears before the definition of g()
        and you'll need to insert a separate declaration of g() somewhere
        before f() in the file.
        >
        --
        Eric.Sos...@sun .com
        Thanks Eric for such a clear explanation. While I get your attention,
        can we take a look at "extern" as well? :) If we have all exported
        functions and global variables included in a header file, does
        it mean that there is really no need to use extern (in the module
        that needs to access those functions and global variables) as we
        can simply #include the header file? What are the needs/advantages
        of using extern?

        /Why Tea

        Comment

        • s0suk3@gmail.com

          #5
          Re: Static function prototype

          On Oct 3, 1:28 pm, Why Tea <ytlim1@gmail.c omwrote:
          On Oct 3, 11:59 am, Eric Sosman <Eric.Sos...@su n.comwrote:
          <snip>
          Thanks Eric for such a clear explanation. While I get your attention,
          can we take a look at "extern" as well? :) If we have all exported
          functions and global variables included in a header file, does
          it mean that there is really no need to use extern (in the module
          that needs to access those functions and global variables) as we
          can simply #include the header file? What are the needs/advantages
          of using extern?
          None. The 'extern' storage class in the declaration/definition of a
          function serves no purpose whatsoever. 'extern' suggests that the
          function has external linkage, but that is the default for functions
          anyway. In other words, declaring/defining a function to be 'extern'
          is like defining a local variable to be 'auto'.

          Sebastian

          Comment

          • Jack Klein

            #6
            Re: Static function prototype

            On Fri, 3 Oct 2008 14:17:34 -0700 (PDT), s0suk3@gmail.co m wrote in
            comp.lang.c:
            On Oct 3, 1:28 pm, Why Tea <ytlim1@gmail.c omwrote:
            On Oct 3, 11:59 am, Eric Sosman <Eric.Sos...@su n.comwrote:
            <snip>
            Thanks Eric for such a clear explanation. While I get your attention,
            can we take a look at "extern" as well? :) If we have all exported
            functions and global variables included in a header file, does
            it mean that there is really no need to use extern (in the module
            that needs to access those functions and global variables) as we
            can simply #include the header file? What are the needs/advantages
            of using extern?
            >
            None. The 'extern' storage class in the declaration/definition of a
            function serves no purpose whatsoever. 'extern' suggests that the
            function has external linkage, but that is the default for functions
            anyway. In other words, declaring/defining a function to be 'extern'
            is like defining a local variable to be 'auto'.
            The extern keyword in C does not specify a storage class, it specifies
            a linkage type. It can only be applied to objects with static storage
            duration, or to functions. But it does not in itself confer any sort
            of storage duration on objects, merely requires that they already have
            static storage duration.

            --
            Jack Klein
            Home: http://JK-Technology.Com
            FAQs for
            comp.lang.c http://c-faq.com/
            comp.lang.c++ http://www.parashift.com/c++-faq-lite/
            alt.comp.lang.l earn.c-c++

            Comment

            • Malcolm McLean

              #7
              Re: Static function prototype

              "Antoninus Twink" <nospam@nospam. invalidwrote in message
              >
              Have you considered using a more sophisticated editor than MS NotePad?
              >
              For a long time I did all my programming from the Visual Studio GUI. You get
              into the way of working with it. It's a very intimate thing, a bit like
              driving a car.

              Then times change, a Beowulf cluster appears, and suddenly that delightful
              GUI is taken away. It's actually better to get into the habit of typing
              things into simple ASCII editors and using printf() as your debugger. Now I
              use emacs. it is a tremendously powerful editor, but I am quite incapable of
              doing anything clever with it. I close it down and use grep when I need to
              search for a function.

              --
              Free games and programming goodies.


              Comment

              • CBFalconer

                #8
                Re: Static function prototype

                Malcolm McLean wrote:
                "Antoninus Twink" <nospam@nospam. invalidwrote in message
                >
                >Have you considered using a more sophisticated editor than MS
                >NotePad?
                >
                For a long time I did all my programming from the Visual Studio
                GUI. You get into the way of working with it. It's a very
                intimate thing, a bit like driving a car.
                >
                .... snip ...

                --
                +-------------------+ .:\:\:/:/:.
                | PLEASE DO NOT F :.:\:\:/:/:.:
                | FEED THE TROLLS | :=.' - - '.=:
                | | '=(\ 9 9 /)='
                | Thank you, | ( (_) )
                | Management | /`-vvv-'\
                +-------------------+ / \
                | | @@@ / /|,,,,,|\ \
                | | @@@ /_// /^\ \\_\
                @x@@x@ | | |/ WW( ( ) )WW
                \||||/ | | \| __\,,\ /,,/__
                \||/ | | | jgs (______Y______)
                /\/\/\/\/\/\/\/\//\/\\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
                =============== =============== =============== =============== ==

                fix (vb.): 1. to paper over, obscure, hide from public view; 2.
                to work around, in a way that produces unintended consequences
                that are worse than the original problem. Usage: "Windows ME
                fixes many of the shortcomings of Windows 98 SE". - Hutchinson

                Comment

                • Giorgos Keramidas

                  #9
                  Re: Static function prototype

                  On Sat, 4 Oct 2008 16:11:29 +0100, "Malcolm McLean" <regniztar@btin ternet.comwrote :
                  >"Antoninus Twink" <nospam@nospam. invalidwrote in message
                  >Have you considered using a more sophisticated editor than MS NotePad?
                  >
                  For a long time I did all my programming from the Visual Studio
                  GUI. You get into the way of working with it. It's a very intimate
                  thing, a bit like driving a car.
                  >
                  Then times change, a Beowulf cluster appears, and suddenly that
                  delightful GUI is taken away. It's actually better to get into the
                  habit of typing things into simple ASCII editors and using printf() as
                  your debugger. Now I use emacs. it is a tremendously powerful editor,
                  but I am quite incapable of doing anything clever with it. I close it
                  down and use grep when I need to search for a function.
                  This is off-topic for comp.lang.c, so I've set the `Followup-To' header
                  appropriately.

                  Emacs is, indeed, a powerful editing application. It may be interesting
                  for your editing sessions to read the following pages of the Emacs Wiki:




                  The Emacs manual describes various ways of launching grep-like searches
                  from within Emacs too. See the section ``Searching with Grep under
                  Emacs'' in the manual.

                  In recent Emacs releases (i.e. 22.1 or later) you can open this section
                  by typing

                  C-h r i grep RET

                  The `C-h r' keys will launch the Emacs manual in Info mode, and the rest
                  of the key sequence (the `i grep RET' keys) will open the grep section
                  by looking up the term `grep' in the index of the manual.

                  Please feel free to ask about other useful things you want to do with
                  your Emacs installations, by posting email questions to

                  help-gnu-emacs@gnu.org

                  or by posting your questions through Usenet to the `news:gnu.emacs .help'
                  group.

                  Have fun with your Emacs installations,
                  Giorgos

                  Comment

                  • Nick Keighley

                    #10
                    Re: Static function prototype

                    On 4 Oct, 08:06, CBFalconer <cbfalco...@yah oo.comwrote:

                    <snip>
                    I invariably foul up the usage of 'declared' versus 'defined'.  At
                    least I know what I mean. :-)
                    me too. In fact I'm half convinced that Cs usage is the opposite
                    of some other language I used (pascal?). This is why I the definition
                    of definition written on the wall. I check my wall everytime
                    I post about definion or declaration.

                    My practice is to declare everything before use, if possible.
                    yes. Old pascal habbit.
                    Obviously mutual recursion alters this.  But that avoids ever
                    needing to maintain two source chunks to have the identical
                    meaning.
                    The code equivalent of Normal Form.


                    --
                    Nick Keighley

                    "The key, the whole key and nothing but the key,
                    So thank me Codd"


                    Comment

                    • CBFalconer

                      #11
                      Re: Static function prototype

                      Nick Keighley wrote:
                      CBFalconer <cbfalco...@yah oo.comwrote:
                      >
                      .... snip ...
                      >
                      >My practice is to declare everything before use, if possible.
                      >
                      yes. Old pascal habbit.
                      I maintain that ex-ISO-Pascalers write better organized code. :-)

                      --
                      [mail]: Chuck F (cbfalconer at maineline dot net)
                      [page]: <http://cbfalconer.home .att.net>
                      Try the download section.

                      Comment

                      • Ben Pfaff

                        #12
                        Re: Static function prototype

                        CBFalconer <cbfalconer@yah oo.comwrites:
                        Nick Keighley wrote:
                        >CBFalconer <cbfalco...@yah oo.comwrote:
                        >>My practice is to declare everything before use, if possible.
                        >>
                        >yes. Old pascal habbit.
                        >
                        I maintain that ex-ISO-Pascalers write better organized code. :-)
                        Like this?
                        while ((EOF != (ch = getchar()))} && isspace(ch)) continue;
                        Ugh. It even has a stray }.
                        --
                        "Some programming practices beg for errors;
                        this one is like calling an 800 number
                        and having errors delivered to your door."
                        --Steve McConnell

                        Comment

                        • Antoninus Twink

                          #13
                          Re: Static function prototype

                          On 6 Oct 2008 at 21:10, Ben Pfaff wrote:
                          CBFalconer <cbfalconer@yah oo.comwrites:
                          >I maintain that ex-ISO-Pascalers write better organized code. :-)
                          >
                          Like this?
                          while ((EOF != (ch = getchar()))} && isspace(ch)) continue;
                          Ugh. It even has a stray }.
                          Yup - CBF's train-wreck style of coding is an affront to taste and
                          decency.

                          Hopefully there won't be any impressionable newbies who follow his
                          example and create hard-to-read, hard-to-debug monstrosities like the
                          line above.

                          Comment

                          • CBFalconer

                            #14
                            Re: Static function prototype

                            Ben Pfaff wrote:
                            CBFalconer <cbfalconer@yah oo.comwrites:
                            >Nick Keighley wrote:
                            >>CBFalconer <cbfalco...@yah oo.comwrote:
                            >>>
                            >>>My practice is to declare everything before use, if possible.
                            >>>
                            >>yes. Old pascal habbit.
                            >>
                            >I maintain that ex-ISO-Pascalers write better organized code. :-)
                            >
                            Like this?
                            while ((EOF != (ch = getchar()))} && isspace(ch)) continue;
                            Ugh. It even has a stray }.
                            That's a mistyped ')'. The statement itself isn't allowed in
                            Pascal, because assignments can't be propagated in expressions.
                            Also eof is a condition, not a macro value.

                            --
                            [mail]: Chuck F (cbfalconer at maineline dot net)
                            [page]: <http://cbfalconer.home .att.net>
                            Try the download section.

                            Comment

                            • Ben Pfaff

                              #15
                              Re: Static function prototype

                              CBFalconer <cbfalconer@yah oo.comwrites:
                              Ben Pfaff wrote:
                              >CBFalconer <cbfalconer@yah oo.comwrites:
                              >>Nick Keighley wrote:
                              >>>CBFalconer <cbfalco...@yah oo.comwrote:
                              >>>>
                              >>>>My practice is to declare everything before use, if possible.
                              >>>>
                              >>>yes. Old pascal habbit.
                              >>>
                              >>I maintain that ex-ISO-Pascalers write better organized code. :-)
                              >>
                              >Like this?
                              > while ((EOF != (ch = getchar()))} && isspace(ch)) continue;
                              >Ugh. It even has a stray }.
                              >
                              That's a mistyped ')'. The statement itself isn't allowed in
                              Pascal, because assignments can't be propagated in expressions.
                              Also eof is a condition, not a macro value.
                              Um, that's your own code, from article <48EA7C6E.148A0 444@yahoo.com>.
                              --
                              char a[]="\n .CJacehknorstu" ;int putchar(int);in t main(void){unsi gned long b[]
                              ={0x67dffdff,0x 9aa9aa6a,0xa77f fda9,0x7da6aa6a ,0xa67f6aaa,0xa a9aa9f6,0x11f6} ,*p
                              =b,i=24;for(;p+ =!*p;*p/=4)switch(0[p]&3)case 0:{return 0;for(p--;i--;i--)case+
                              2:{i++;if(i)bre ak;else default:continu e;if(0)case 1:putchar(a[i&15]);break;}}}

                              Comment

                              Working...