Variable declaration Vs Definition

Discussion in 'C Programming' started by manish sahu, Sep 26, 2009.

  1. manish sahu

    manish sahu Guest

    Is there any diff between Variable declaration and definition??

    Whatever i understood is that
    in case of auto,static and register storage class
    variable declaration is its definition
    (bcoz we are allocation memory while declaration( in register storage
    cpu registers) for the variables

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



    but in case of external storage class variable declaration and
    definition are different . In external storage class declaration
    does'nt allocate memory space for variable it is done by variable
    definition.
    ex:

    int main()
    {
    extern int a; //declaration
    printf("%d",a);
    return 0;
    }
    int a=10; // definition

    Is it correct ?
    PLz Reply
    Thanks to all
     
    manish sahu, Sep 26, 2009
    #1
    1. Advertising

  2. manish sahu

    Tim Rentsch Guest

    Richard Heathfield <> writes:

    > In
    > <>,
    > manish sahu wrote:
    >
    >> Is there any diff between Variable declaration and definition??

    >
    > Declaration is a promise that an object (or function) has been
    > created. Definition is an instruction to create it. Therefore,
    > definitions effectively count as declarations, but not vice versa.


    A possible clarification:

    Because of how terminology is used in the ISO Standard,
    in a function like this --

    int
    foo(){
    int x;
    x = 3;
    return x;
    }

    the line 'int x;' is a "definition" as the Standard uses
    the term, even though many or most developers would refer to
    this line just as a declaration.
     
    Tim Rentsch, Oct 2, 2009
    #2
    1. Advertising

  3. On Sat, 26 Sep 2009 00:22:00 -0700 (PDT), manish sahu
    <> wrote:

    > Is there any diff between Variable declaration and definition??
    >

    Yes. And you've got the basic idea right, but not entirely.

    > Whatever i understood is that
    > in case of auto,static and register storage class
    > variable declaration is its definition
    > (bcoz we are allocation memory while declaration( in register storage
    > cpu registers) for the variables
    >

    For auto and register, yes, the declaration is always a definition,
    which allocates space during the execution of the function or block
    containing the definition. The standard doesn't specify how, but in
    practice it is usually in CPU registers or in the region of memory at
    the logical top (usually physical bottom) of the CPU-defined stack.
    Long long ago it could be important which variables you declared
    register; nowadays the compiler will usually choose better than you
    which variables should (or can) be kept in registers, and often only
    for part(s) of their lifetimes.

    > int main()
    > {
    > int i;
    > printf("%d",i);
    > }
    >
    >
    >
    > but in case of external storage class variable declaration and
    > definition are different . In external storage class declaration
    > does'nt allocate memory space for variable it is done by variable
    > definition.


    For both 'static' and 'extern' there can be a referring (nondefining)
    declaration as well as a definition. However, for 'static' these can
    only occur within one source file (or to be exact translation unit,
    but that difference is minor and you can ignore it for now). For
    'extern' these can be in different source files, and usually should.

    Aside: what actually matters is internal or external 'linkage'. The
    specifiers 'static' and 'extern' in a declaration usually map to
    these, but not in some odd cases; I'll leave that out for now.

    > ex:
    >
    > int main()
    > {
    > extern int a; //declaration
    > printf("%d",a);
    > return 0;
    > }
    > int a=10; // definition
    >
    > Is it correct ?


    That is a correct way, but:

    (1) it is conventional to put declarations of non-local variables only
    at file scope (that is, outside of any function). Especially if they
    are in #include'd files, as they usually should be to allow easier
    maintenance. The only reason you need to put one within a local scope
    is if you are doing tricky things with shadowing (hiding) names, which
    is usually a bad idea (and certainly not where you should start).

    (2) the *benefit* of 'extern' is mostly in using multiple source files
    (translation units). A more demonstrative example would be:

    // file main.c
    extern int zork;
    int main ()
    {
    printf ("%d\n", zork);
    return 0;
    }

    // file zork.c
    int zork = 42;
     
    David Thompson, Oct 5, 2009
    #3
    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:
    494
    John Harrison
    Aug 7, 2003
  2. Kristian Virkus
    Replies:
    7
    Views:
    423
    Thad Smith
    Feb 8, 2007
  3. Bolin
    Replies:
    4
    Views:
    420
  4. vaib

    variable declaration and definition!

    vaib, Oct 5, 2007, in forum: C Programming
    Replies:
    17
    Views:
    546
  5. Pierre Yves
    Replies:
    2
    Views:
    524
    Pierre Yves
    Jan 10, 2008
Loading...

Share This Page