difference between declaration and defination....

Discussion in 'C' started by An007kit, Oct 2, 2007.

  1. An007kit

    An007kit New Member

    Joined:
    Oct 1, 2007
    Messages:
    15
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Am a student
    Location:
    Live in chandigarh, india
    Can anyone help me with the concept of declaration and definition...and what is the difference between the two....?
    is the statement :-
    float y;
    a declaration or definition.....
     
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    As far as I know the concept of declaration and definition applies to function where you declare the function and then define its body at later stage.
     
  3. buddy

    buddy New Member

    Joined:
    Aug 31, 2007
    Messages:
    22
    Likes Received:
    0
    Trophy Points:
    0
    Declaration of a variable does not have a physical existance,that is memory will not be allocated.where as defining a variable means memory will be allocated.
     
  4. DaWei

    DaWei New Member

    Joined:
    Dec 6, 2006
    Messages:
    835
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Semi-retired EE
    Location:
    Texan now in Central NY
    Home Page:
    http://www.daweidesigns.com
    That is incorrect, buddy. Declaration of a variable DOES set aside memory for the variable.

    A class or struct variable will not be allocated memory until the object is instantiated, unless that member is static, then memory will be allocated for it.

    If one declares a variable as 'extern', then that variable is not part of the current file scope, but is presumed to be declared (and exist) elsewhere. If it does not, the linker will generate an error.

    Declaration of a function provides the compiler with a signature for the function (often referred to as a prototype), but does not occupy memory.
     
  5. buddy

    buddy New Member

    Joined:
    Aug 31, 2007
    Messages:
    22
    Likes Received:
    0
    Trophy Points:
    0
    Thank u Dawei,

    clear explaination.ive posted that answer with doubt.now got cleared.good things comes from discussion. but how can we explain the difference between declaration and definition in simple form.
     
  6. DaWei

    DaWei New Member

    Joined:
    Dec 6, 2006
    Messages:
    835
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Semi-retired EE
    Location:
    Texan now in Central NY
    Home Page:
    http://www.daweidesigns.com
    For a variable, declaration and definition are much the same thing. The variable is declared to exist and its type is defined.

    Depending upon circumstances (where the variable is declared), the variable may or may not receive some known initial value.

    For a function, the declaration provides the signature of the function, prior to use, so that the compiler will know if subsequent calls are written correctly. That's all the compiler needs to know. The definition of the function generates the actual code, which must exist at link time and run time.

    If the function definition (the writing of the actual code) occurs before it is first encountered by the compiler, then no separate declaration is needed, as the compiler can determing the signature from the definition.
     
  7. asit

    asit New Member

    Joined:
    Jan 10, 2007
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Declaration of a variable makes the attributes of that variable known to compiler.
    e.g. int fact(int );
    in this case fact is a function and this statement makes the declaration of the function fact. compiler only knows the attributes of fact.

    Defination of a variable not only makes the attributes of that variable known to compiler but also memory is allocated for that variable.
    e.g: int a;
    the attributes of a is known to compileras well as 4 byte is allocated to a.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice