c definition and declaration

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • himanshumodi
    New Member
    • Aug 2008
    • 1

    c definition and declaration

    plz tell me that in c..
    whether
    int a;
    is declaration or definition??
    will memory be allocated at the time it is written...
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    It is a tentative definition; I normally use the declaration 'extern int a' in a header
    file and use one tentative definition in exactly one .c file: 'int a'.

    kind regards,

    Jos

    Comment

    • scottdavie
      New Member
      • Aug 2008
      • 13

      #3
      memory will be allocated. Although you have set no number, a number will be stored in a. If you

      cout << a << endl;

      after int a; then a number is still displayed.

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        Nothing tentative about it. Memory is allocated. That makes it a definition. It's not initialized to any particular value but that is irrelevant.

        As a definition, it is not supposed to be in a header file.

        Comment

        • JosAH
          Recognized Expert MVP
          • Mar 2007
          • 11453

          #5
          Originally posted by weaknessforcats
          Nothing tentative about it.
          Yes it is; see the C99 Standard for this (I can't quote it here because it is all
          over the place).

          kind regards,

          Jos

          Comment

          • weaknessforcats
            Recognized Expert Expert
            • Mar 2007
            • 9214

            #6
            Well, there you go, I gave a C++ answer again on a C topic.

            Comment

            • scottdavie
              New Member
              • Aug 2008
              • 13

              #7
              They should only be defined in header files if they are part of a function or a class. Otherwise I dont believe they should be.

              Comment

              • weaknessforcats
                Recognized Expert Expert
                • Mar 2007
                • 9214

                #8
                I repeat, definitions do not belong in header files.

                Every time you include that header you get another definition. Multiple definitions will cause problems at link time.

                Comment

                • scottdavie
                  New Member
                  • Aug 2008
                  • 13

                  #9
                  Originally posted by weaknessforcats
                  I repeat, definitions do not belong in header files.

                  Every time you include that header you get another definition. Multiple definitions will cause problems at link time.
                  Using #ifndef would prevent this problem

                  Comment

                  • edwardrsmith
                    New Member
                    • Feb 2008
                    • 62

                    #10
                    But that doesn't mean that it is good style. By including definitions in a header file you are also slowing down compile time. The entire reason that there are header files and libraries is that way it is unnecessary to compile the entire library every time it is used. This would be incredibly tome consuming and a pain. Instead a header file contains the decleration of the function (and often the documentation for the function as well) while the library contains the compiled but unlinked definitions.

                    If you search for header files in google you will get a lot of good examples of this.

                    Edward

                    Comment

                    • JosAH
                      Recognized Expert MVP
                      • Mar 2007
                      • 11453

                      #11
                      Originally posted by scottdavie
                      Using #ifndef would prevent this problem
                      No it wouldn't; different compilation units can include the same header file
                      (with a definition in it) and there is no #ifndef that can prevent another definition
                      in another compilation unit.

                      kind regards,

                      Jos

                      Comment

                      • edwardrsmith
                        New Member
                        • Feb 2008
                        • 62

                        #12
                        Originally posted by edwardrsmith
                        But that doesn't mean that it is good style. By including definitions in a header file you are also slowing down compile time. The entire reason that there are header files and libraries is that way it is unnecessary to compile the entire library every time it is used. This would be incredibly tome consuming and a pain. Instead a header file contains the decleration of the function (and often the documentation for the function as well) while the library contains the compiled but unlinked definitions.

                        If you search for header files in google you will get a lot of good examples of this.

                        Edward
                        EDIT: I am not saying that it is unnecessary to use ifndef but instead am saying that ifndef is only part of the solution.

                        Sorry: hit the wrong button and didn't notice.

                        Comment

                        • edwardrsmith
                          New Member
                          • Feb 2008
                          • 62

                          #13
                          I think he was talking about using ifndef combined with define in the format of

                          Code:
                          #ifndef test_h
                          #define test_h
                          
                          // Code
                          
                          #endif
                          This would prevent it from being defined twice as it would only be compiled once.

                          Edward

                          Comment

                          • JosAH
                            Recognized Expert MVP
                            • Mar 2007
                            • 11453

                            #14
                            Originally posted by edwardrsmith
                            I think he was talking about using ifndef combined with define in the format of

                            Code:
                            #ifndef test_h
                            #define test_h
                            
                            // Code
                            
                            #endif
                            This would prevent it from being defined twice as it would only be compiled once.

                            Edward
                            Sure but it can't prevent multiple definitions over multiple translation units.
                            Multiple defintions in one translation unit are signalled by the compiler.

                            kind regards,

                            Jos

                            Comment

                            Working...