1. We have moved from vBulletin to XenForo and you are viewing the site in the middle of the move. Though the functional aspect of everything is working fine, we are still working on other changes including the new design on Xenforo.
    Dismiss Notice

difference between declaration and defination....

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

  1. An007kit

    An007kit New Member

    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

    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

    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

    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

    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

    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

    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