what is the difference between declaration and defination?

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

    what is the difference between declaration and defination?

    what is the difference between declaration and defination?
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    If you look up the noun "promise" in the Meriam Webster dictionary you find this:

    1 a: a declaration that one will do or refrain from doing something specified b: a legally binding declaration that gives the person to whom it is made a right to expect or to claim the performance or forbearance of a specified act
    2: reason to expect something <little promise of relief>; especially : ground for expectation of success, improvement, or excellence <shows considerable promise>
    3: something that is promised
    That is exactly what a declaration is: a promise that some identifier exists,
    somewhere and with a particular type and if there is more to that identifier
    (as in a function) that is promised (i.e. declared) as well.

    A definition is nothing more than a fulfillment of that promise.

    kind regards,

    Jos

    Comment

    • weaknessforcats
      Recognized Expert Expert
      • Mar 2007
      • 9214

      #3
      Or to put it more clearly: If it occupies memory then it is a definition. Otherwise, it's a declaration.

      Comment

      • questionit
        Contributor
        • Feb 2007
        • 553

        #4
        weaknessforcats -

        int a;

        is it not occupying memory? what does it do if it doesn't.

        does it atleast reserve required portion of memory - as using it.?

        if so, then how the above statement can become definition - by this e.g:
        [code=c]
        int a =10;
        [/code]

        Please clarify

        Qi



        Originally posted by weaknessforcats
        Or to put it more clearly: If it occupies memory then it is a definition. Otherwise, it's a declaration.
        Last edited by Ganon11; Aug 30 '08, 06:21 PM. Reason: Fixing [CODE] tags.

        Comment

        • Banfa
          Recognized Expert Expert
          • Feb 2006
          • 9067

          #5
          Originally posted by weaknessforcats
          Or to put it more clearly: If it occupies memory then it is a definition. Otherwise, it's a declaration.
          But a structure or class definition creates and occupies no memory or is that too a declaration?

          Comment

          • Banfa
            Recognized Expert Expert
            • Feb 2006
            • 9067

            #6
            Originally posted by questionit
            int a;

            is it not occupying memory? what does it do if it doesn't.

            does it atleast reserve required portion of memory - as using it.?

            if so, then how the above statement can become definition - by this e.g:
            [code=c]
            int a =10;
            [/code]
            Assuming that the int a; is not inside a class or structure but rather is global or inside a function then this does occupy memory and is a definition.

            Adding the = 10 does not make it a definition, it was always a definition all it does is add an initialisation to the definition.

            A declaration would look something like

            extern int a;

            Comment

            • JosAH
              Recognized Expert MVP
              • Mar 2007
              • 11453

              #7
              Originally posted by Banfa
              But a structure or class definition creates and occupies no memory or is that too a declaration?
              Yep, it's a structure declaration, not a definition (for reasons of consistency in
              the definition (sic) of the names). C gets quite fuzzy about it sometimes; read
              their specification of "tentative" definitions:

              Originally posted by C Standard
              6.9.2 External object definitions

              Semantics

              [#1] If the declaration of an identifier for an object has
              file scope and an initializer, the declaration is an
              external definition for the identifier.

              [#2] A declaration of an identifier for an object that has
              file scope without an initializer, and without a storage-
              class specifier or with the storage-class specifier static,
              constitutes a tentative definition. If a translation unit
              contains one or more tentative definitions for an
              identifier, and the translation unit contains no external
              definition for that identifier, then the behavior is exactly
              as if the translation unit contains a file scope declaration
              of that identifier, with the composite type as of the end of
              the translation unit, with an initializer equal to 0.

              [#3] If the declaration of an identifier for an object is a
              tentative definition and has internal linkage, the declared
              type shall not be an incomplete type.

              [#4] EXAMPLE 1

              int i1 = 1; // definition, external linkage
              static int i2 = 2; // definition, internal linkage
              extern int i3 = 3; // definition, external linkage
              int i4; // tentative definition, external linkage
              static int i5; // tentative definition, internal linkage

              int i1; // valid tentative definition, refers to previous
              int i2; // 6.2.2 renders undefined, linkage disagreement
              int i3; // valid tentative definition, refers to previous
              int i4; // valid tentative definition, refers to previous
              int i5; // 6.2.2 renders undefined, linkage disagreement

              extern int i1; // refers to previous, whose linkage is external
              extern int i2; // refers to previous, whose linkage is internal
              extern int i3; // refers to previous, whose linkage is external
              extern int i4; // refers to previous, whose linkage is external
              extern int i5; // refers to previous, whose linkage is internal

              [#5] EXAMPLE 2 If at the end of the translation unit
              containing

              int i[];

              the array i still has incomplete type, the implicit
              initializer causes it to have one element, which is set to
              zero on program startup.
              Isn't C a fun language? ;-)

              kind regards,

              Jos

              Comment

              Working...