Definition vs Declaration

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

    Definition vs Declaration

    I have following code .
    #include<stdio. h>
    int i;
    int i;
    int i;

    int main()
    {
    printf("%d",i);
    return 0;
    }

    It compiles fine .

    now i modify above code to

    #include<stdio. h>
    int i = 0;
    int i;
    int i;

    int main()
    {
    printf("%d",i);
    return 0;
    }

    again it compiles fine.

    now i introduce more change to above code

    #include<stdio. h>
    int i = 0;
    int i = 0;
    int i;

    int main()
    {
    printf("%d",i);
    return 0;
    }

    It gives me redefination Error.

    Ok it means that statement 'int i;' is just a declaration. I guess, it
    is not just a declaration since our very first code(code with all 'int
    i;') compiled with no error. There exists atleast one defination of
    'i'. what should we say to statement 'int i;' ?

  • Richard Heathfield

    #2
    Re: Definition vs Declaration

    shaanxxx said:
    I have following code .
    #include<stdio. h>
    int i;
    Tentative definition.
    int i;
    Tentative definition.
    int i;
    Tentative definition.

    Why three?
    It compiles fine .
    Yes, - but... why?
    now i modify above code to
    >
    #include<stdio. h>
    int i = 0;
    Definition and initialisation.
    int i;
    Tentative definition.
    int i;
    Tentative definition.

    But... *why*?
    >
    int main()
    {
    printf("%d",i);
    return 0;
    }
    >
    again it compiles fine.
    >
    now i introduce more change to above code
    >
    #include<stdio. h>
    int i = 0;
    Definition and initialisation.
    int i = 0;
    Definition and initialisation. That's two for-sure definitions with the same
    name, which is a for-sure error. And it has to be asked... WHY?
    int i;
    Tentative definition.
    int main()
    {
    printf("%d",i);
    return 0;
    }
    >
    It gives me redefination Error.
    >
    Ok it means that statement 'int i;' is just a declaration. I guess, it
    is not just a declaration since our very first code(code with all 'int
    i;') compiled with no error. There exists atleast one defination of
    'i'. what should we say to statement 'int i;' ?
    "Don't do it" is the obvious answer, surely?

    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999

    email: rjh at above domain (but drop the www, obviously)

    Comment

    • Fred Kleinschmidt

      #3
      Re: Definition vs Declaration


      "shaanxxx" <shaanxxx@yahoo .comwrote in message
      news:1158770083 .233678.223350@ i42g2000cwa.goo glegroups.com.. .
      >I have following code .
      #include<stdio. h>
      int i;
      int i;

      My compilers all say:
      error: redeclaration of 'i'
      int i;
      >
      int main()
      {
      printf("%d",i);
      return 0;
      }
      >
      It compiles fine .
      ><snip>
      Get a new compiler
      --
      Fred L. Kleinschmidt
      Boeing Associate Technical Fellow
      Technical Architect, Software Reuse Project


      Comment

      • Harald van Dijk

        #4
        Re: Definition vs Declaration

        Fred Kleinschmidt wrote:
        "shaanxxx" <shaanxxx@yahoo .comwrote in message
        news:1158770083 .233678.223350@ i42g2000cwa.goo glegroups.com.. .
        I have following code .
        #include<stdio. h>
        int i;
        int i;
        >
        My compilers all say:
        error: redeclaration of 'i'
        Your compilers are all non-conforming, then.

        Comment

        • Michael Mair

          #5
          Re: Definition vs Declaration

          Fred Kleinschmidt schrieb:
          "shaanxxx" <shaanxxx@yahoo .comwrote
          >
          >>I have following code .
          >>#include<stdi o.h>
          >>int i;
          >>int i;
          >
          My compilers all say:
          error: redeclaration of 'i'
          Then all your compilers are wrong.
          >>int i;
          >>
          >>int main()
          >>{
          >>printf("%d",i );
          >>return 0;
          >>}
          >>
          >>It compiles fine .
          >><snip>
          >
          Get a new compiler
          Maybe you got confused with the difference between declarations
          with file scope and linkage and with block scope and no linkage:
          ,---
          #include<stdio. h>
          int i;
          int i;

          int main (void)
          {
          extern int j;
          int j = 10;
          int k;
          int k = 10;
          printf("%d\n",i );
          printf("%d\n",j );
          printf("%d\n",k );
          return 0;
          }

          `---
          $ gcc -std=c89 -pedantic -Wall -O tentative.c -c
          tentative.c: In function `main':
          tentative.c:10: error: redeclaration of 'k' with no linkage
          tentative.c:9: error: previous declaration of 'k' was here
          tentative.c:9: warning: unused variable `k'


          Cheers
          Michael
          --
          E-Mail: Mine is an /at/ gmx /dot/ de address.

          Comment

          • Martin Ambuhl

            #6
            Re: Definition vs Declaration

            Fred Kleinschmidt wrote:
            "shaanxxx" <shaanxxx@yahoo .comwrote in message
            news:1158770083 .233678.223350@ i42g2000cwa.goo glegroups.com.. .
            >I have following code .
            >#include<stdio .h>
            >int i;
            >int i;
            >
            >
            My compilers all say:
            error: redeclaration of 'i'
            You are posting to <news:comp.lang .c>. You are using compilers for a
            different language, probably C++, which has a newsgroup of its own,
            <news:comp.lang .c++>. Perhaps you should learn to use "My compilers all."

            Comment

            • Fred Kleinschmidt

              #7
              Re: Definition vs Declaration


              "Martin Ambuhl" <mambuhl@earthl ink.netwrote in message
              news:4ndmirFa01 j2U1@individual .net...
              Fred Kleinschmidt wrote:
              >"shaanxxx" <shaanxxx@yahoo .comwrote in message
              >news:115877008 3.233678.223350 @i42g2000cwa.go oglegroups.com. ..
              >>I have following code .
              >>#include<stdi o.h>
              >>int i;
              >>int i;
              >>
              >>
              >My compilers all say:
              >error: redeclaration of 'i'
              >
              You are posting to <news:comp.lang .c>. You are using compilers for a
              different language, probably C++, which has a newsgroup of its own,
              <news:comp.lang .c++>. Perhaps you should learn to use "My compilers all."
              I am using:
              gcc on CYGWIN on a PC
              /opt/ansic/bin/cc on HP 11.11
              /usr/ibmcc/bin/xlc on AIX 5.3
              /usr/bin/cc on IRIX 6.5
              gcc on SunOS5.8
              --
              Fred L. Kleinschmidt


              Comment

              • Martin Ambuhl

                #8
                Re: Definition vs Declaration

                Fred Kleinschmidt wrote:
                "Martin Ambuhl" <mambuhl@earthl ink.netwrote in message
                news:4ndmirFa01 j2U1@individual .net...
                >Fred Kleinschmidt wrote:
                >>"shaanxxx" <shaanxxx@yahoo .comwrote in message
                >>news:11587700 83.233678.22335 0@i42g2000cwa.g ooglegroups.com ...
                >>>I have following code .
                >>>#include<std io.h>
                >>>int i;
                >>>int i;
                >>>
                >>My compilers all say:
                >>error: redeclaration of 'i'
                >You are posting to <news:comp.lang .c>. You are using compilers for a
                >different language, probably C++, which has a newsgroup of its own,
                ><news:comp.lan g.c++>. Perhaps you should learn to use "My compilers all."
                >
                I am using:
                gcc on CYGWIN on a PC
                /opt/ansic/bin/cc on HP 11.11
                /usr/ibmcc/bin/xlc on AIX 5.3
                /usr/bin/cc on IRIX 6.5
                gcc on SunOS5.8
                You are invoking it as a C++ compiler. When GCC is invoked as a C
                compiler, it does not give the diagnostic you report. Please learn to
                use "My compilers all" or post your C++ observations to
                <news:comp.lang .c++>, not <news:comp.lang .c>.

                Comment

                • Ark

                  #9
                  Re: Definition vs Declaration

                  Richard Heathfield wrote:
                  shaanxxx said:
                  >
                  >I have following code .
                  >#include<stdio .h>
                  >int i;
                  >
                  Tentative definition.
                  >
                  >int i;
                  >
                  Tentative definition.
                  >
                  >int i;
                  >
                  Tentative definition.
                  >
                  Why three?
                  >
                  >It compiles fine .
                  >
                  Yes, - but... why?
                  >
                  >now i modify above code to
                  >>
                  >#include<stdio .h>
                  >int i = 0;
                  >
                  Definition and initialisation.
                  >
                  >int i;
                  >
                  Tentative definition.
                  >
                  >int i;
                  >
                  Tentative definition.
                  >
                  But... *why*?
                  >
                  >int main()
                  >{
                  >printf("%d",i) ;
                  >return 0;
                  >}
                  >>
                  >again it compiles fine.
                  >>
                  >now i introduce more change to above code
                  >>
                  >#include<stdio .h>
                  >int i = 0;
                  >
                  Definition and initialisation.
                  >
                  >int i = 0;
                  >
                  Definition and initialisation. That's two for-sure definitions with the same
                  name, which is a for-sure error. And it has to be asked... WHY?
                  >
                  >int i;
                  >
                  Tentative definition.
                  >
                  >int main()
                  >{
                  >printf("%d",i) ;
                  >return 0;
                  >}
                  >>
                  >It gives me redefination Error.
                  >>
                  >Ok it means that statement 'int i;' is just a declaration. I guess, it
                  >is not just a declaration since our very first code(code with all 'int
                  >i;') compiled with no error. There exists atleast one defination of
                  >'i'. what should we say to statement 'int i;' ?
                  >
                  "Don't do it" is the obvious answer, surely?
                  >
                  The OP correctly distilled the case of his predicament to a minimum.
                  That's a satisfactory answer to "why".
                  [I am sure Mr. Heathfield does not question the general utility of
                  tentative definitions <OTunfortunatel y removed from C++ </OT>.]
                  - Ark

                  Comment

                  • Richard Heathfield

                    #10
                    Re: Definition vs Declaration

                    Ark said:

                    <snip>
                    [I am sure Mr. Heathfield does not question the general utility of
                    tentative definitions
                    I don't? Why on earth not?

                    --
                    Richard Heathfield
                    "Usenet is a strange place" - dmr 29/7/1999

                    email: rjh at above domain (but drop the www, obviously)

                    Comment

                    • Ark

                      #11
                      Re: Definition vs Declaration

                      Richard Heathfield wrote:
                      Ark said:
                      >
                      <snip>
                      >
                      >[I am sure Mr. Heathfield does not question the general utility of
                      >tentative definitions
                      >
                      I don't? Why on earth not?
                      >
                      Oh, you do then.
                      Here's what I use them for.
                      Consider a data arrangement of elements linking to one another (a
                      directed graph that is) such that there are loops of links as graph
                      edges. The simplest thing is (where QUAL is a set of qualifiers)
                      typedef QUAL struct loop_t {
                      QUAL struct loop_t *next;
                      SOMETYPE *data;
                      } loop_t;

                      loop_t A; //tentative
                      loop_t B = {&A, B_Data};
                      loop_t C = {&B, C_Data};
                      loop_t A = {&C, A_Data);

                      That technique is *very* useful in e.g. embedded systems, especially
                      when QUAL is static const (or static: the point is that you don't have
                      extern to plug the hole of a forward reference).

                      <OTGranted, one can perhaps arrange the data better (e.g. by using
                      indices into a single array), but to do it in a maintainable way one
                      needs an external preprocessor. </OT>

                      Regards,
                      Ark

                      Comment

                      • Richard Heathfield

                        #12
                        Re: Definition vs Declaration

                        Ark said:
                        Richard Heathfield wrote:
                        >Ark said:
                        >>
                        ><snip>
                        >>
                        >>[I am sure Mr. Heathfield does not question the general utility of
                        >>tentative definitions
                        >>
                        >I don't? Why on earth not?
                        >>
                        Oh, you do then.
                        Here's what I use them for.
                        I'm not denying one can find some kind of bizarre use for them. You can find
                        a use for a glass slipper if you try hard enough. (Indeed, there are some
                        indications that this may already have been done.) That doesn't mean that
                        glass slippers are generally useful or wise items of footwear. Similarly,
                        the fact that it is possible to find a use for a tentative definition does
                        not mean that they are generally useful. Nor does it mean that there is not
                        some other way to achieve the same goal.

                        --
                        Richard Heathfield
                        "Usenet is a strange place" - dmr 29/7/1999

                        email: rjh at above domain (but drop the www, obviously)

                        Comment

                        • Ark

                          #13
                          Re: Definition vs Declaration

                          Richard Heathfield wrote:
                          Ark said:
                          >
                          >Richard Heathfield wrote:
                          >>Ark said:
                          >>>
                          >><snip>
                          >>>
                          >>>[I am sure Mr. Heathfield does not question the general utility of
                          >>>tentative definitions
                          >>I don't? Why on earth not?
                          >>>
                          >Oh, you do then.
                          >Here's what I use them for.
                          >
                          I'm not denying one can find some kind of bizarre use for them. You can find
                          a use for a glass slipper if you try hard enough. (Indeed, there are some
                          indications that this may already have been done.) That doesn't mean that
                          glass slippers are generally useful or wise items of footwear. Similarly,
                          the fact that it is possible to find a use for a tentative definition does
                          not mean that they are generally useful. Nor does it mean that there is not
                          some other way to achieve the same goal.
                          >
                          1. AFAIK, a glass slipper is a misnomer resulting from an error in the
                          first translation from Old French; it meant to be a fur slipper. The
                          intention of this remark is to demonstrate that even you might not be
                          infallible.
                          2. Of course, you, like everyone else, are free not to use faculties you
                          are not comfortable with. However, it doesn't seem appropriate to pass
                          baseless value judgments.
                          3. In particular, w.r.t. 'bizarre', I believe the C++ standard
                          elaborates on removing tentative definitions and explicitly discusses an
                          example of a circular data structure like in the example I gave, that is
                          expressible in C but not in C++.

                          --
                          Ark Khasin
                          "Life is hard but short"
                          akhasin at macroexpression s dot com
                          www.macroexpressions.com where Unimal comes from

                          Comment

                          • Richard Heathfield

                            #14
                            Re: Definition vs Declaration

                            Ark said:
                            Richard Heathfield wrote:
                            >Ark said:
                            >>
                            >>Richard Heathfield wrote:
                            >>>Ark said:
                            >>>>
                            >>><snip>
                            >>>>
                            >>>>[I am sure Mr. Heathfield does not question the general utility of
                            >>>>tentative definitions
                            >>>I don't? Why on earth not?
                            >>>>
                            >>Oh, you do then.
                            >>Here's what I use them for.
                            >>
                            >I'm not denying one can find some kind of bizarre use for them. You can
                            >find a use for a glass slipper if you try hard enough. (Indeed, there are
                            >some indications that this may already have been done.) That doesn't mean
                            >that glass slippers are generally useful or wise items of footwear.
                            >Similarly, the fact that it is possible to find a use for a tentative
                            >definition does not mean that they are generally useful. Nor does it mean
                            >that there is not some other way to achieve the same goal.
                            >>
                            1. AFAIK, a glass slipper is a misnomer resulting from an error in the
                            first translation from Old French; it meant to be a fur slipper.
                            That may or may not be true, but it has no bearing on my point.
                            The intention of this remark is to demonstrate that even you might not be
                            infallible.
                            I have never claimed to be infallible, but your remark fails to demonstrate
                            that I am.
                            2. Of course, you, like everyone else, are free not to use faculties you
                            are not comfortable with. However, it doesn't seem appropriate to pass
                            baseless value judgments.
                            I have passed no value judgements on other people. If others wish to use
                            tentative definitions, that is entirely up to them. I am, however,
                            perfectly entitled to make a value judgement about whether *I* consider
                            tentative definitions to be sufficiently useful to compensate for their
                            counter-intuitive nature.
                            3. In particular, w.r.t. 'bizarre', I believe the C++ standard
                            elaborates on removing tentative definitions and explicitly discusses an
                            example of a circular data structure like in the example I gave, that is
                            expressible in C but not in C++.
                            If one is prepared to set aside constness, the data structure you showed is
                            perfectly expressible in C++, and indeed in C, without the use of tentative
                            definitions. It is even possible to regain constness to some extent, by
                            setting up the data structure as part of an abstract data type, and
                            refraining from providing interfaces that allow the modification of that
                            structure from outside the type's implementation.

                            --
                            Richard Heathfield
                            "Usenet is a strange place" - dmr 29/7/1999

                            email: rjh at above domain (but drop the www, obviously)

                            Comment

                            • Al Balmer

                              #15
                              Re: Definition vs Declaration

                              On Fri, 22 Sep 2006 12:59:03 -0400, Ark <akhasin@macroe xpressions.com>
                              wrote:
                              <OT>
                              >1. AFAIK, a glass slipper is a misnomer resulting from an error in the
                              >first translation from Old French; it meant to be a fur slipper. The
                              >intention of this remark is to demonstrate that even you might not be
                              >infallible.
                              Actually, the French mis-translated it from the Chinese - the slipper
                              was embroidered silk.

                              Of course, all these were merely precursors of the true Cinderella,
                              invented by Walt Disney.

                              More seriously, the 1697 French version (Charles Perrault) actually
                              said glass (verre), though some scholars are of the opinion that
                              Perrault actually meant fur (vair).
                              </OT>

                              --
                              Al Balmer
                              Sun City, AZ

                              Comment

                              Working...