Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   difference between declaration and defination.... (http://www.go4expert.com/forums/difference-declaration-defination-t6646/)

An007kit 2Oct2007 16:10

difference between declaration and defination....
 
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.....

shabbir 2Oct2007 18:19

Re: difference between declaration and defination....
 
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.

buddy 2Oct2007 19:30

Re: difference between declaration and defination....
 
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.

DaWei 2Oct2007 20:52

Re: difference between declaration and defination....
 
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.

buddy 2Oct2007 21:17

Re: difference between declaration and defination....
 
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.

DaWei 2Oct2007 21:55

Re: difference between declaration and defination....
 
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.

asit 9Oct2007 00:31

Re: difference between declaration and defination....
 
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.


All times are GMT +5.5. The time now is 23:48.