Is there a real need to use keyword static with functions?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • lcdgoncalves@gmail.com

    Is there a real need to use keyword static with functions?

    Hi everyone

    Is there a real need to use keyword static with functions, if we
    simply don't declare their prototypes in .h file?

    Many textbooks avoid to discuss this matter and/or discuss only the
    usage of static functions. I've been programming for years and I never
    felt a real need for the "static approach for functions".

    I don't know if this is a trivial matter since the reserved word
    exists. :)
    Or it exists for some other purpose I can't figure out.

    What did I have missed? :)

    Thanks in advance for answers

  • Ian Collins

    #2
    Re: Is there a real need to use keyword static with functions?

    lcdgoncalves@gm ail.com wrote:
    Hi everyone
    >
    Is there a real need to use keyword static with functions, if we
    simply don't declare their prototypes in .h file?
    >
    Yes, otherwise the functions have global scope and pollute the global
    namespace. You may not want to export those symbols and you, or the
    user of you code, will run into link problems if the same name is used
    in more than one source file.

    --
    Ian Collins.

    Comment

    • christian.bau

      #3
      Re: Is there a real need to use keyword static with functions?

      On Mar 5, 9:10 pm, lcdgoncal...@gm ail.com wrote:
      Many textbooks avoid to discuss this matter and/or discuss only the
      usage of static functions. I've been programming for years and I never
      felt a real need for the "static approach for functions".
      That's sad.

      Do you have a database of all functions in your programs to make sure
      that you don't use the same function name twice?

      Comment

      • Ben Pfaff

        #4
        Re: Is there a real need to use keyword static with functions?

        lcdgoncalves@gm ail.com writes:
        Is there a real need to use keyword static with functions, if we
        simply don't declare their prototypes in .h file?
        Yes. If two functions are defined without static, but with the
        same name, in different translation units, then the result is
        undefined behavior. Using static avoids this undefined behavior.
        --
        "C has its problems, but a language designed from scratch would have some too,
        and we know C's problems."
        --Bjarne Stroustrup

        Comment

        • Kenny McCormack

          #5
          Re: Is there a real need to use keyword static with functions?

          In article <87649fl71z.fsf @blp.benpfaff.o rg>,
          Ben Pfaff <blp@cs.stanfor d.eduwrote:
          >lcdgoncalves@g mail.com writes:
          >
          >Is there a real need to use keyword static with functions, if we
          >simply don't declare their prototypes in .h file?
          >
          >Yes. If two functions are defined without static, but with the
          >same name, in different translation units, then the result is
          >undefined behavior. Using static avoids this undefined behavior.
          Again, you guys are "talking around the problem". What you should be
          reacting to is the implication (i.e., the OP's delusion) that neglecting
          to declare a prototype in a header file somehow "hides" global symbols
          found in translation units. I.e., makes it as if they had been declared
          (gasp!) static.

          Well, to the OP, 'taint so.

          Comment

          • user923005

            #6
            Re: Is there a real need to use keyword static with functions?

            On Mar 5, 1:10 pm, lcdgoncal...@gm ail.com wrote:
            Hi everyone
            >
            Is there a real need to use keyword static with functions, if we
            simply don't declare their prototypes in .h file?
            >
            Many textbooks avoid to discuss this matter and/or discuss only the
            usage of static functions. I've been programming for years and I never
            felt a real need for the "static approach for functions".
            >
            I don't know if this is a trivial matter since the reserved word
            exists. :)
            Or it exists for some other purpose I can't figure out.
            >
            What did I have missed? :)
            >
            Thanks in advance for answers
            Functions should always be made static if they are not intended as end-
            user available routines.
            Imagine some function called :
            void HelperInitializ er(struct goombah * foo);
            and it is only supposed to be called by *you* the programmer. It is
            to be called only once and if it were to be called again, it would
            reset the state of some of your objects in a way not intended. It is
            essential to define this function as static or it *will* be available
            to the end-user of the library whether you like it or not.

            There are lots of other sensible reasons for static (some of which are
            mentioned else-thread).

            The author of that book wouldn't have been Herbert Schildt, would it?

            Comment

            • Richard Tobin

              #7
              Re: Is there a real need to use keyword static with functions?

              In article <1173129046.926 414.36190@c51g2 000cwc.googlegr oups.com>,
              <lcdgoncalves@g mail.comwrote:
              >Is there a real need to use keyword static with functions, if we
              >simply don't declare their prototypes in .h file?
              Bear in mind that there's nothing magic about .h files. They're just
              some C code - usually, but not necessarily declarations - that can be
              handily included in other files. So the fact that a function isn't
              declared in a .h file doesn't stop some other part of the program from
              using it, or from inadvertently using the same name for something else
              (which may be detected when you link the program, or may just cause
              some peculiar failure later on).

              -- Richard
              --
              "Considerat ion shall be given to the need for as many as 32 characters
              in some alphabets" - X3.4, 1963.

              Comment

              • Erik de Castro Lopo

                #8
                Re: Is there a real need to use keyword static with functions?

                lcdgoncalves@gm ail.com wrote:
                Hi everyone
                >
                Is there a real need to use keyword static with functions, if we
                simply don't declare their prototypes in .h file?
                Not necessary, but good practice.

                If you use static then you can have two C files which both have
                their own definition of my_function and the compiler will accept
                it and do the right thing.

                If you don't use static you will get a link time error about two
                definitions of my_function.

                HTH,
                Erik
                --
                +-----------------------------------------------------------+
                Erik de Castro Lopo
                +-----------------------------------------------------------+
                Failure is not an option. It comes bundled with your Microsoft
                product.

                Comment

                • pradeep

                  #9
                  Re: Is there a real need to use keyword static with functions?

                  Hi everyone,
                  Is there any real need for keyword "static inline" with
                  functions.

                  --pradeep.
                  On Mar 6, 3:59 am, Erik de Castro Lopo <e...@mega-nerd.comwrote:
                  lcdgoncal...@gm ail.com wrote:
                  Hi everyone
                  >
                  Is there a real need to use keyword static with functions, if we
                  simply don't declare their prototypes in .h file?
                  >
                  Not necessary, but good practice.
                  >
                  If you use static then you can have two C files which both have
                  their own definition of my_function and the compiler will accept
                  it and do the right thing.
                  >
                  If you don't use static you will get a link time error about two
                  definitions of my_function.
                  >
                  HTH,
                  Erik
                  --
                  +-----------------------------------------------------------+
                  Erik de Castro Lopo
                  +-----------------------------------------------------------+
                  Failure is not an option. It comes bundled with your Microsoft
                  product.

                  Comment

                  • santosh

                    #10
                    Re: Is there a real need to use keyword static with functions?

                    pradeep wrote:

                    Please don't top-post. Your reply should be below or interspersed with
                    the material you quote. Irrelevant text can be deleted, particularly
                    sig blocks that follow a '-- ' marker.
                    [fixed]
                    On Mar 6, 3:59 am, Erik de Castro Lopo <e...@mega-nerd.comwrote:
                    lcdgoncal...@gm ail.com wrote:
                    Hi everyone
                    Is there a real need to use keyword static with functions, if we
                    simply don't declare their prototypes in .h file?
                    Not necessary, but good practice.

                    If you use static then you can have two C files which both have
                    their own definition of my_function and the compiler will accept
                    it and do the right thing.

                    If you don't use static you will get a link time error about two
                    definitions of my_function.
                    Hi everyone,
                    Is there any real need for keyword "static inline" with
                    functions.
                    Well, static's effect has been explained in this thread. The inline
                    qualifier is a hint to the compiler to speed up access to the
                    function, if possible. Generally, it's used for very small functions
                    that're called in tight loops. The compiler may or may not actually
                    embed the function's body into each place where it's invoked. There
                    may be other methods of speeding up access to it.

                    inline is a C99 feature, unlike static.

                    Comment

                    • santosh

                      #11
                      Re: Is there a real need to use keyword static with functions?


                      pradeep wrote:
                      Hi everyone,
                      Is there any real need for keyword "static inline" with
                      functions.
                      BTW, using inline has many caveats that you need to be aware of.
                      >From the working draft of ISO C Standard:
                      6.7.4 Function specifiers
                      Syntax
                      1 function-specifier:
                      inline
                      Constraints
                      2 Function specifiers shall be used only in the declaration of an
                      identifier for a function.
                      3 An inline definition of a function with external linkage shall not
                      contain a definition of a
                      modifiable object with static storage duration, and shall not contain
                      a reference to an
                      identifier with internal linkage.
                      4 In a hosted environment, the inline function specifier shall not
                      appear in a declaration
                      of main.
                      Semantics
                      5 A function declared with an inline function specifier is an inline
                      function. The
                      function specifier may appear more than once; the behavior is the
                      same as if it appeared
                      only once. Making a function an inline function suggests that calls
                      to the function be as
                      fast as possible.118) The extent to which such suggestions are
                      effective is
                      implementation-defined.119)
                      6 Any function with internal linkage can be an inline function. For a
                      function with external
                      linkage, the following restrictions apply: If a function is declared
                      with an inline
                      function specifier, then it shall also be defined in the same
                      translation unit. If all of the
                      file scope declarations for a function in a translation unit include
                      the inline function
                      specifier without extern, then the definition in that translation unit
                      is an inline
                      definition. An inline definition does not provide an external
                      definition for the function,
                      and does not forbid an external definition in another translation
                      unit. An inline definition
                      provides an alternative to an external definition, which a translator
                      may use to implement
                      any call to the function in the same translation unit. It is
                      unspecified whether a call to the
                      function uses the inline definition or the external definition.120 )

                      Comment

                      • jaysome

                        #12
                        Re: Is there a real need to use keyword static with functions?

                        On 5 Mar 2007 13:10:47 -0800, lcdgoncalves@gm ail.com wrote:
                        >Hi everyone
                        >
                        >Is there a real need to use keyword static with functions, if we
                        >simply don't declare their prototypes in .h file?
                        >
                        >Many textbooks avoid to discuss this matter and/or discuss only the
                        >usage of static functions. I've been programming for years and I never
                        >felt a real need for the "static approach for functions".
                        >
                        >I don't know if this is a trivial matter since the reserved word
                        >exists. :)
                        >Or it exists for some other purpose I can't figure out.
                        >
                        >What did I have missed? :)
                        >
                        >Thanks in advance for answers
                        In C, functions can have one of two types of linkage: external or
                        internal.

                        A function with external linkage can be defined only once and called
                        by multiple translation units, while a function with internal linkage
                        can be defined once for each translation unit and called (directly)
                        only by the translation unit that defines it (separate translation
                        units that define the same function with internal linkage effectively
                        get their own copy of the function definition).

                        The keyword static, when used in a function prototype or definition,
                        implies internal linkage, while the keyword extern (or lack of both
                        extern and static) in a function prototype or definition, implies
                        external linkage

                        All functions should have a prototype in scope, with the exception of
                        main().

                        Function prototypes declared in a .h file should not be declared as
                        static; you can declare them as extern, but that's not necessary.

                        /* foo.h */
                        static void foo1(void); /* bad */
                        void foo2(void); /* okay */
                        extern foo3(void); /* okay, though use of extern is superfluous */

                        Function prototypes declared in a .c file should use static. The
                        function definition whose prototype is declared with static can also
                        use static (but not extern), but that's not necessary, although it's
                        good practice.

                        /* foo.c */
                        static void foo4(void);

                        #ifdef DEFS_WITH_STATI C
                        static void foo4(void) /* okay */
                        {
                        /* implement foo4 */
                        }
                        #else
                        void foo4(void) /* okay */
                        {
                        /* implement foo4 */
                        }
                        #endif

                        Likewise, function definitions whose prototype is not declared with
                        static can also use extern (but not static), but that's not necessary,
                        although it's considered acceptable by some and suspect by others
                        (PC-lint falls into the latter category).

                        /* foo.c */
                        #include "foo.h"
                        #ifdef DEFS_WITH_EXTER N
                        extern void foo2(void) /* okay */
                        {
                        /* implement foo2 */
                        }
                        #else
                        void foo2(void) /* okay */
                        {
                        /* implement foo2 */
                        }
                        #endif

                        One argument for not using extern with function definitions is that
                        you can "grep" a .c source file for "extern" and expect to find no
                        matches.

                        Regards
                        --
                        jay

                        It's here!
                        Experience the latest Microsoft Windows 11 features. Learn how our latest Windows OS gives you more ways to work, play, and create.

                        Comment

                        • CBFalconer

                          #13
                          Re: Is there a real need to use keyword static with functions?

                          santosh wrote:
                          pradeep wrote:
                          >
                          Please don't top-post. Your reply should be below or interspersed
                          with the material you quote. Irrelevant text can be deleted,
                          particularly sig blocks that follow a '-- ' marker.
                          [fixed]
                          >
                          .... snip ...

                          IIRC he's been told this before, and ignored it. Unless there are
                          multiple pradeeps extant, he is due to be plonked.

                          --
                          <http://www.cs.auckland .ac.nz/~pgut001/pubs/vista_cost.txt>
                          <http://www.securityfoc us.com/columnists/423>

                          "A man who is right every time is not likely to do very much."
                          -- Francis Crick, co-discover of DNA
                          "There is nothing more amazing than stupidity in action."
                          -- Thomas Matthews


                          Comment

                          • Roland Pibinger

                            #14
                            Re: Is there a real need to use keyword static with functions?

                            On 5 Mar 2007 13:41:06 -0800, "user923005 " <dcorbit@connx. comwrote:
                            >Functions should always be made static if they are not intended as end-
                            >user available routines.
                            IOW, static functions in C serve the same purpose as 'private'
                            funtions are in other languages.

                            Best wishes,
                            Roland Pibinger

                            Comment

                            • santosh

                              #15
                              Re: Is there a real need to use keyword static with functions?


                              Roland Pibinger wrote:
                              On 5 Mar 2007 13:41:06 -0800, "user923005 " <dcorbit@connx. comwrote:
                              Functions should always be made static if they are not intended as end-
                              user available routines.
                              >
                              IOW, static functions in C serve the same purpose as 'private'
                              funtions are in other languages.
                              Maybe in some senses of private. For example, C++'s private member
                              functions are quite different from C's static qualified functions.

                              Comment

                              Working...