Definition vs Declaration

Discussion in 'C Programming' started by shaanxxx, Sep 20, 2006.

  1. shaanxxx

    shaanxxx Guest

    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;' ?
    shaanxxx, Sep 20, 2006
    #1
    1. Advertising

  2. 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
    http://www.cpax.org.uk
    email: rjh at above domain (but drop the www, obviously)
    Richard Heathfield, Sep 20, 2006
    #2
    1. Advertising

  3. "shaanxxx" <> wrote in message
    news:...
    >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
    Fred Kleinschmidt, Sep 20, 2006
    #3
  4. Fred Kleinschmidt wrote:
    > "shaanxxx" <> wrote in message
    > news:...
    > >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.
    =?utf-8?B?SGFyYWxkIHZhbiBExLNr?=, Sep 20, 2006
    #4
  5. shaanxxx

    Michael Mair Guest

    Fred Kleinschmidt schrieb:
    > "shaanxxx" <> wrote
    >
    >>I have following code .
    >>#include<stdio.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.
    Michael Mair, Sep 20, 2006
    #5
  6. Fred Kleinschmidt wrote:
    > "shaanxxx" <> wrote in message
    > news:...
    >> 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."
    Martin Ambuhl, Sep 20, 2006
    #6
  7. "Martin Ambuhl" <> wrote in message
    news:...
    > Fred Kleinschmidt wrote:
    >> "shaanxxx" <> wrote in message
    >> news:...
    >>> 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."


    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
    Fred Kleinschmidt, Sep 20, 2006
    #7
  8. Fred Kleinschmidt wrote:
    > "Martin Ambuhl" <> wrote in message
    > news:...
    >> Fred Kleinschmidt wrote:
    >>> "shaanxxx" <> wrote in message
    >>> news:...
    >>>> 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."

    >
    > 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>.
    Martin Ambuhl, Sep 20, 2006
    #8
  9. shaanxxx

    Ark Guest

    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 <OT> unfortunately removed from C++ </OT>.]
    - Ark
    Ark, Sep 21, 2006
    #9
  10. 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
    http://www.cpax.org.uk
    email: rjh at above domain (but drop the www, obviously)
    Richard Heathfield, Sep 21, 2006
    #10
  11. shaanxxx

    Ark Guest

    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).

    <OT> Granted, 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
    Ark, Sep 22, 2006
    #11
  12. 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
    http://www.cpax.org.uk
    email: rjh at above domain (but drop the www, obviously)
    Richard Heathfield, Sep 22, 2006
    #12
  13. shaanxxx

    Ark Guest

    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 macroexpressions dot com
    www.macroexpressions.com where Unimal comes from
    Ark, Sep 22, 2006
    #13
  14. 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
    http://www.cpax.org.uk
    email: rjh at above domain (but drop the www, obviously)
    Richard Heathfield, Sep 22, 2006
    #14
  15. shaanxxx

    Al Balmer Guest

    On Fri, 22 Sep 2006 12:59:03 -0400, Ark <>
    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
    Al Balmer, Sep 22, 2006
    #15
  16. shaanxxx

    Default User Guest

    Richard Heathfield wrote:

    > Ark said:


    > > 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.



    OT, but it seems likely that it's not true. Some had speculated on such
    an error, but there's little evidence of it.




    Brian
    Default User, Sep 22, 2006
    #16
  17. shaanxxx

    Ark Guest

    Richard Heathfield wrote:
    <snip>

    > I have passed no value judgements on other people.

    <snip>
    Please, kindly re-read your advice to the OP.

    >
    >> 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,
    Please don't get me wrong: I, too, do not find tentative definitions
    particularly elegant. They do the job though.
    I would really appreciate your elaborating on representing cyclical
    dependencies without tentative definitions (generally and with const if
    possible since that's important for resource consumption considerations).
    If you are correct and an equivalent job can be done by other means, I'd
    be more than happy to abandon them tentatives

    --
    Ark Khasin
    "Life is hard but short"
    akhasin at macroexpressions dot com
    www.macroexpressions.com where Unimal comes from
    Ark, Sep 22, 2006
    #17
  18. Ark said:

    > Richard Heathfield wrote:
    > <snip>
    >
    >> I have passed no value judgements on other people.

    > <snip>
    > Please, kindly re-read your advice to the OP.


    Q: "It gives me redefination Error. [...] what should we say to statement
    'int i;' ?"

    A: '"Don't do it" is the obvious answer, surely?'

    My advice to the OP is clear - if it gives you a redefinition error, don't
    do it. I stand by that advice, and it is not a value judgement on a person.
    It is, perhaps, a value judgement on erroneous code. <shrug>

    > If you are correct and an equivalent job can be done by other means, I'd
    > be more than happy to abandon them tentatives


    It's entirely up to you. No skin off my nose.

    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999
    http://www.cpax.org.uk
    email: rjh at above domain (but drop the www, obviously)
    Richard Heathfield, Sep 23, 2006
    #18
  19. shaanxxx

    Ark Guest

    Richard Heathfield wrote:
    > Ark said:
    >
    >> Richard Heathfield wrote:
    >> <snip>
    >>
    >>> I have passed no value judgements on other people.

    >> <snip>
    >> Please, kindly re-read your advice to the OP.

    >
    > Q: "It gives me redefination Error. [...] what should we say to statement
    > 'int i;' ?"
    >
    > A: '"Don't do it" is the obvious answer, surely?'
    >
    > My advice to the OP is clear - if it gives you a redefinition error, don't
    > do it. I stand by that advice, and it is not a value judgement on a person.
    > It is, perhaps, a value judgement on erroneous code. <shrug>
    >

    I meant, all the way, judgment on language constructs

    >> If you are correct and an equivalent job can be done by other means, I'd
    >> be more than happy to abandon them tentatives

    >
    > It's entirely up to you. No skin off my nose.
    >

    Oh yes I tried. And the only thing that works was using tentatives. If
    you care to show a different way, please do so. Until then, the
    tentatives shall be considered vindicated.

    --
    Ark Khasin
    "Life is hard but short"
    akhasin at macroexpressions dot com
    www.macroexpressions.com where Unimal comes from
    Ark, Sep 23, 2006
    #19
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Razvan
    Replies:
    4
    Views:
    613
    Razvan
    Feb 10, 2005
  2. Jianli Shen
    Replies:
    1
    Views:
    568
    Victor Bazarov
    Mar 13, 2005
  3. Ark
    Replies:
    1
    Views:
    405
    Chris Torek
    Aug 7, 2004
  4. Jon Slaughter
    Replies:
    4
    Views:
    433
    Jon Slaughter
    Oct 26, 2005
  5. Pierre Yves
    Replies:
    2
    Views:
    474
    Pierre Yves
    Jan 10, 2008
Loading...

Share This Page