variable declaration and definition!

Discussion in 'C Programming' started by vaib, Oct 5, 2007.

  1. vaib

    vaib Guest

    hi to all.i'd like to know the actual difference between variable
    declaration and definition.it would be very helpful if anyone out
    there wud help me out with this thing.i'm writing here after here
    after a long time since people here also helped me out with my lexer.
    thanking in anticipation.
     
    vaib, Oct 5, 2007
    #1
    1. Advertising

  2. vaib

    santosh Guest

    vaib wrote:

    > hi to all.i'd like to know the actual difference between variable
    > declaration and definition.it would be very helpful if anyone out
    > there wud help me out with this thing.i'm writing here after here
    > after a long time since people here also helped me out with my lexer.
    > thanking in anticipation.


    Declarations and definitions apply to both objects, (the C Standard's term
    for what others might refer to as variables), and functions. A declaration
    specifies the properties of the object or function to the compiler while a
    definition causes an actual instance of an object to be created or output
    to be generated in the case of functions.

    For example:

    struct foo {
    int x;
    struct bar *pbar;
    };

    is a declaration of an aggregate type called foo. After this declaration the
    compiler has enough "knowledge" to properly handle other instances of foo
    occurring in the source.

    struct foo foo_arr[10];

    is a definition. It causes actual storage to be set aside for an array of
    ten foo_arr objects, each of which is of type foo.

    Obviously a definition has to follow a declaration. You can also combine a
    declaration with a definition as in:

    struct foo {
    int x;
    struct bar *pbar;
    } foo_x, foo_y;

    A function declaration announces the return type, name, and number and type
    of parameters the function accepts. This is to enable proper resolution of
    forward references and external linkages. A function definition, i.e., the
    actual code of the function, also serves as a declaration.

    Objects and functions defined in other translation units usually require a
    proper declaration before they can be used.

    For more details consult a good text on C like _The C Programming Language_
    (2nd Edition) by Kernighan & Ritchie or _C: A Reference Manual_ (5th
    Edition) by Harbison & Steele.

    Also see:
    <http://www.c-faq.com/>
    <http://www.clc-wiki.net/>
    <http://www.eskimo.com/~scs/cclass/>
    <http://cprog.tomsweb.net/>
     
    santosh, Oct 5, 2007
    #2
    1. Advertising

  3. On Fri, 05 Oct 2007 00:25:17 -0700, in comp.lang.c , vaib
    <> wrote:

    >hi to all.i'd like to know the actual difference between variable
    >declaration and definition.


    In C, a declaration of X says "there is an object called X, and this
    is its type". It doesn't create any storage for the object. You can
    have many declarations of an object in a programme (as long as they're
    all the same).

    A definition however creates the actual object. There can be only one
    of it.

    You can think of declarations like entries in a Table of Contents of a
    book. It tells you there will be a volume 5 chapter 3. You can have
    multiple tables of contents, say in different volumes, but only one
    actual chapter, the definition.
    --
    Mark McIntyre

    "Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are,
    by definition, not smart enough to debug it."
    --Brian Kernighan
     
    Mark McIntyre, Oct 5, 2007
    #3
  4. vaib

    Richard Guest

    Mark McIntyre <> writes:

    > On Fri, 05 Oct 2007 00:25:17 -0700, in comp.lang.c , vaib
    > <> wrote:
    >
    >>hi to all.i'd like to know the actual difference between variable
    >>declaration and definition.

    >
    > In C, a declaration of X says "there is an object called X, and this
    > is its type". It doesn't create any storage for the object. You can
    > have many declarations of an object in a programme (as long as they're
    > all the same).


    This is news to me. For years I laboured under the understanding that
    the declaration created storage too.

    int x; /* declare variable x to be of type int */
     
    Richard, Oct 5, 2007
    #4
  5. Richard said:

    <snip>

    > For years I laboured under the understanding that
    > the declaration created storage too.


    That's not an understanding; it's a misunderstanding. A definition reserves
    storage. All definitions are declarations, but not all declarations are
    definitions.

    --
    Richard Heathfield <http://www.cpax.org.uk>
    Email: -http://www. +rjh@
    Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
    "Usenet is a strange place" - dmr 29 July 1999
     
    Richard Heathfield, Oct 5, 2007
    #5
  6. vaib

    osmium Guest

    "Richard" writes:

    > Mark McIntyre <> writes:


    >> In C, a declaration of X says "there is an object called X, and this
    >> is its type". It doesn't create any storage for the object. You can
    >> have many declarations of an object in a programme (as long as they're
    >> all the same).

    >
    > This is news to me. For years I laboured under the understanding that
    > the declaration created storage too.
    >
    > int x; /* declare variable x to be of type int */


    That's what the people who drool over such arcane things call a "tentative
    definition". Here's one early hit on a web search.

    http://publib.boulder.ibm.com/infoc...p6m.doc/language/ref/clrc03tentative_defn.htm
     
    osmium, Oct 5, 2007
    #6
  7. vaib

    Richard Guest

    "osmium" <> writes:

    > "Richard" writes:
    >
    >> Mark McIntyre <> writes:

    >
    >>> In C, a declaration of X says "there is an object called X, and this
    >>> is its type". It doesn't create any storage for the object. You can
    >>> have many declarations of an object in a programme (as long as they're
    >>> all the same).

    >>
    >> This is news to me. For years I laboured under the understanding that
    >> the declaration created storage too.
    >>
    >> int x; /* declare variable x to be of type int */

    >
    > That's what the people who drool over such arcane things call a "tentative
    > definition". Here's one early hit on a web search.
    >
    > http://publib.boulder.ibm.com/infoc...p6m.doc/language/ref/clrc03tentative_defn.htm


    Really, I am amazed and quite prepared to be officially "wrong", but in
    20 years or so of programming I have always used "declare x" as opposed
    to "define x".
     
    Richard, Oct 5, 2007
    #7
  8. On Fri, 05 Oct 2007 16:18:03 +0000, in comp.lang.c , Richard
    Heathfield <> wrote:

    >A definition reserves storage.


    Unless its the definition of a struct or union?

    struct foo
    {
    int bar;
    }; // definition ? or declaration of type?

    struct foo foostruct1; // declaration and definition

    void f() {
    struct foo foostruct2; // declaration

    };


    --
    Mark McIntyre

    "Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are,
    by definition, not smart enough to debug it."
    --Brian Kernighan
     
    Mark McIntyre, Oct 5, 2007
    #8
  9. Mark McIntyre <> writes:
    > On Fri, 05 Oct 2007 16:18:03 +0000, in comp.lang.c , Richard
    > Heathfield <> wrote:
    >>A definition reserves storage.


    Specifically, an object definition reserves storage.

    > Unless its the definition of a struct or union?
    >
    > struct foo
    > {
    > int bar;
    > }; // definition ? or declaration of type?


    The standard calls that a declaration, not a definition (if I'm
    reading the grammar correctly). (I probably would have called it a
    definition, since it creates the type, but it's not a big deal; either
    way, it doesn't reserver any storage.)

    > struct foo foostruct1; // declaration and definition


    Right, that declares and defines an object.

    > void f() {
    > struct foo foostruct2; // declaration


    That also declares and defines an object (that happens to have
    automatic storage duration).

    If you had written:

    extern struct foo whatever;

    that would be a declaration, not a definition.

    > };


    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Oct 6, 2007
    #9
  10. vaib

    Martin Wells Guest

    On Oct 5, 8:25 am, vaib <> wrote:
    > hi to all.i'd like to know the actual difference between variable
    > declaration and definition.it would be very helpful if anyone out
    > there wud help me out with this thing.i'm writing here after here
    > after a long time since people here also helped me out with my lexer.
    > thanking in anticipation.



    int i; /* Extern, deFinition */

    int i = 7; /* Extern, deFinition */

    extern int i; /* Extern, deClaration */

    extern int i = 7; /* Extern, deFinition */

    static int j; /* Static, deClaration */

    static int j = 7; /* Static, deFinition */


    Quite funky indeed.

    Martin
     
    Martin Wells, Oct 6, 2007
    #10
  11. On Fri, 05 Oct 2007 16:29:10 -0700, in comp.lang.c , Keith Thompson
    <> wrote:

    >Mark McIntyre <> writes:


    >> struct foo foostruct1; // declaration and definition

    >
    >Right, that declares and defines an object.


    at file scope, its a tentative definition I think?
    >
    >> void f() {
    >> struct foo foostruct2; // declaration

    >
    >That also declares and defines an object (that happens to have
    >automatic storage duration).


    oops, you're right - needs extern in front.

    --
    Mark McIntyre

    "Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are,
    by definition, not smart enough to debug it."
    --Brian Kernighan
     
    Mark McIntyre, Oct 6, 2007
    #11
  12. Martin Wells <> writes:

    > On Oct 5, 8:25 am, vaib <> wrote:
    >> hi to all.i'd like to know the actual difference between variable
    >> declaration and definition.


    <snip>
    > static int j; /* Static, deClaration */


    Surely this one is a definition as well? It is a tentative
    definition, but unless anything perturbs it, it acts as if is a real
    one (with an initialiser of 0). Note that your other examples of
    definitions that lacked initialisations were also tentative.

    > Quite funky indeed.


    Slightly less so if the above is as much a definition as "int i;".

    --
    Ben.
     
    Ben Bacarisse, Oct 7, 2007
    #12
  13. Harald van Dijk <> writes:

    > On Sun, 07 Oct 2007 00:59:00 +0100, Ben Bacarisse wrote:
    >> Martin Wells <> writes:
    >>
    >>> On Oct 5, 8:25 am, vaib <> wrote:
    >>>> hi to all.i'd like to know the actual difference between variable
    >>>> declaration and definition.

    >>
    >> <snip>
    >>> static int j; /* Static, deClaration */

    >>
    >> Surely this one is a definition as well?

    >
    > No.
    >
    >> It is a tentative definition,

    >
    > Yes.
    >
    >> but unless anything perturbs it, it acts as if is a real one (with an
    >> initialiser of 0).

    >
    > A tentative definition is not itself a definition.


    Thanks. I thought that a tentative definition could be thought of as
    a kind of definition but I can see that would be confusing and is
    technically wrong.

    "int i;" is also a tentative definition. My point was that Martin
    Wells seemed to be suggesting a difference that does not exist between
    "int i;" and "static int j;" (at least for file scope identifiers).

    --
    Ben.
     
    Ben Bacarisse, Oct 7, 2007
    #13
  14. On Sun, 07 Oct 2007 00:59:00 +0100, Ben Bacarisse wrote:
    > Martin Wells <> writes:
    >
    >> On Oct 5, 8:25 am, vaib <> wrote:
    >>> hi to all.i'd like to know the actual difference between variable
    >>> declaration and definition.

    >
    > <snip>
    >> static int j; /* Static, deClaration */

    >
    > Surely this one is a definition as well?


    No.

    > It is a tentative definition,


    Yes.

    > but unless anything perturbs it, it acts as if is a real one (with an
    > initialiser of 0).


    A tentative definition is not itself a definition.

    static int i[];

    This is a tentative definition, and the implicit initialiser would give
    it a length of 1, but...

    static int i[2];

    ....might appear later. The actual definition (if no explicit definition
    is present) is generated once, regardless of how many tentative
    definitions you have, and the actual definition's type is not necessarily
    the type of any tentative definition, but rather, the composite type of
    all tentative definitions.
     
    =?iso-2022-kr?q?=1B=24=29CHarald_van_D=0E=29=26=0F, Oct 7, 2007
    #14
  15. vaib

    vaib Guest

    thank u all . i hope i got the point . thank u so much .
     
    vaib, Oct 9, 2007
    #15
  16. vaib

    vaib Guest

    On Oct 7, 6:03 am, Ben Bacarisse <> wrote:
    > Harald van D k <> writes:
    >
    >
    >
    > > On Sun, 07 Oct 2007 00:59:00 +0100, Ben Bacarisse wrote:
    > >> Martin Wells <> writes:

    >
    > >>> On Oct 5, 8:25 am, vaib <> wrote:
    > >>>> hi to all.i'd like to know the actual difference between variable
    > >>>> declaration and definition.

    >
    > >> <snip>
    > >>> static int j; /* Static, deClaration */

    >
    > >> Surely this one is a definition as well?

    >
    > > No.

    >
    > >> It is a tentative definition,

    >
    > > Yes.

    >
    > >> but unless anything perturbs it, it acts as if is a real one (with an
    > >> initialiser of 0).

    >
    > > A tentative definition is not itself a definition.

    >
    > Thanks. I thought that a tentative definition could be thought of as
    > a kind of definition but I can see that would be confusing and is
    > technically wrong.
    >
    > "int i;" is also a tentative definition. My point was that Martin
    > Wells seemed to be suggesting a difference that does not exist between
    > "int i;" and "static int j;" (at least for file scope identifiers).
    >
    > --
    > Ben.


    for once it seemed all clear but things have started to get jumbled up
    again.now wats exactly a tentative declaration ?? and how is int i =
    3 ; an external declaration ??
     
    vaib, Oct 9, 2007
    #16
  17. vaib <> writes:

    > On Oct 7, 6:03 am, Ben Bacarisse <> wrote:
    >> Harald van D k <> writes:

    <snip>
    >> > A tentative definition is not itself a definition.

    >>
    >> Thanks. I thought that a tentative definition could be thought of as
    >> a kind of definition but I can see that would be confusing and is
    >> technically wrong.
    >>
    >> "int i;" is also a tentative definition. My point was that Martin
    >> Wells seemed to be suggesting a difference that does not exist between
    >> "int i;" and "static int j;" (at least for file scope identifiers).
    >>
    >> --
    >> Ben.


    Best not to quote sigs (the lines including and after the "-- ").

    > for once it seemed all clear but things have started to get jumbled up
    > again.now wats exactly a tentative declaration ?? and how is int i =
    > 3 ; an external declaration ??


    First, you are confusing 'declaration' and 'definition'. I was
    talking only about definitions and tentative definitions.

    Second, 'int i = 2;' is an external definition of x because the
    standard says it is. A declaration of a file-scope object with an
    initializer constitutes an external definition (6.9.2 p1).

    If a file-scope object is declared without an initializer and without
    a storage class (or with storage class static) it is called a
    tentative definition. E.g. both

    int i;
    static int j;

    are tentative definitions. Why is this? The reason is to allow these
    to co-exist with at most one declaration that *does* include an
    initialiser. The standard wants to allow:

    int x;
    ...
    int x = 42;

    (these can be in either order) and also to allow

    int y;

    on its own[1] with the meaning that y is defined and initialized to 0.
    To do this, they introduce the idea of tentative definition. To have
    simply said that 'int x;' is the same as 'int x = 0;' would have made
    the example above illegal (since x would be defined twice).

    [1] "on its own" means in a translation unit that may have multiple
    tentative definitions of the same identifier but which has no external
    definitions for it.

    --
    Ben.
     
    Ben Bacarisse, Oct 10, 2007
    #17
  18. vaib

    vaib Guest

    On Oct 10, 10:00 pm, Ben Bacarisse <> wrote:
    > vaib <> writes:
    > > On Oct 7, 6:03 am, Ben Bacarisse <> wrote:
    > >> Harald van D k <> writes:

    > <snip>
    > >> > A tentative definition is not itself a definition.

    >
    > >> Thanks. I thought that a tentative definition could be thought of as
    > >> a kind of definition but I can see that would be confusing and is
    > >> technically wrong.

    >
    > >> "int i;" is also a tentative definition. My point was that Martin
    > >> Wells seemed to be suggesting a difference that does not exist between
    > >> "int i;" and "static int j;" (at least for file scope identifiers).

    >
    > >> --
    > >> Ben.

    >
    > Best not to quote sigs (the lines including and after the "-- ").
    >
    > > for once it seemed all clear but things have started to get jumbled up
    > > again.now wats exactly a tentative declaration ?? and how is int i =
    > > 3 ; an external declaration ??

    >
    > First, you are confusing 'declaration' and 'definition'. I was
    > talking only about definitions and tentative definitions.
    >
    > Second, 'int i = 2;' is an external definition of x because the
    > standard says it is. A declaration of a file-scope object with an
    > initializer constitutes an external definition (6.9.2 p1).
    >
    > If a file-scope object is declared without an initializer and without
    > a storage class (or with storage class static) it is called a
    > tentative definition. E.g. both
    >
    > int i;
    > static int j;
    >
    > are tentative definitions. Why is this? The reason is to allow these
    > to co-exist with at most one declaration that *does* include an
    > initialiser. The standard wants to allow:
    >
    > int x;
    > ...
    > int x = 42;
    >
    > (these can be in either order) and also to allow
    >
    > int y;
    >
    > on its own[1] with the meaning that y is defined and initialized to 0.
    > To do this, they introduce the idea of tentative definition. To have
    > simply said that 'int x;' is the same as 'int x = 0;' would have made
    > the example above illegal (since x would be defined twice).
    >
    > [1] "on its own" means in a translation unit that may have multiple
    > tentative definitions of the same identifier but which has no external
    > definitions for it.
    >
    > --
    > Ben.


    thank u so much.i did a little research and got the point.
     
    vaib, Oct 12, 2007
    #18
    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. John Harrison
    Replies:
    4
    Views:
    485
    John Harrison
    Aug 7, 2003
  2. Kristian Virkus
    Replies:
    7
    Views:
    414
    Thad Smith
    Feb 8, 2007
  3. Bolin
    Replies:
    4
    Views:
    415
  4. Pierre Yves
    Replies:
    2
    Views:
    504
    Pierre Yves
    Jan 10, 2008
  5. manish sahu

    Variable declaration Vs Definition

    manish sahu, Sep 26, 2009, in forum: C Programming
    Replies:
    2
    Views:
    1,786
    David Thompson
    Oct 5, 2009
Loading...

Share This Page