| Author |
Message |
sivamit
Joined: 01 Dec 2005 Posts: 113 Location: India
|
23 Apr 2008 12:15 Declaration Vs definition |
|
|
|
Hi what is the diff between these two..?
Can anyone please explain..?
Thanks
Shiva
|
|
| Back to top |
|
 |
yousif
Joined: 02 Dec 2005 Posts: 78 Helped: 6
|
23 Apr 2008 13:24 Re: Declaration Vs definition |
|
|
|
Difference between declaring and defining with functions:
The prototype statement for a function declares it, i.e. tells the compiler about the function - its name, return type, and number and type of its parameters.
The function header, followed by the body of the function, defines the function - giving the details of the steps to perform the function operation.
Ex.
| Code: |
//Declare
int foo(int);
//Define
int foo(int){
...
}
|
With Respect to Variables:
For automatic and register variables, there is no difference between definition and declaration.
The process of declaring an automatic or a register variable defines the variable name and allocates appropriate memory.
However, for external variables:
Because memory for a variable must be allocated only once, to ensure that access to the variable always refers to the same cell.
all variables must be defined once and only once.
If an external variable is to be used in a file other than the one in which it is defined, a mechanism is needed to "connect" such a use with the uniquely defined external variable cell allocated for it. This process of connecting the references of the same external variable in different files, is called resolving the references.
It may be defined and declared with a declaration statement outside any function, with no storage class specifier. Such a declaration allocates memory for the variable. A declaration statement may also be used to simply declare a variable name with the extern storage class specifier at the beginning of the declaration. Such a declaration specifies that the variable is defined elsewhere, i.e. memory for this variable is allocated in another file. Thus, access to an external variable in a file other than the one in which it is defined is possible if it is declared with the keyword extern; no new memory is allocated. Such a declaration tells the compiler that the variable is defined elsewhere, and the code is compiled with the external variable left unresolved. The reference to the external variable is resolved during the linking process.
Ex.
| Code: |
//file1.c
extern char stack[10];
extern int stkptr;
....
//file2.c
char stack[10];
int stkptr;
....
|
These declarations tell the compiler that the variables stack[] and stkptr are defined elsewhere, usually in some other file. If the keyword extern were omitted, the variables would be considered to be new ones and memory would be allocated for them. Remember, access to the same external variable defined in another file is possible only if the keyword extern is used in the declaration.
|
|
| Back to top |
|
 |
H_D_R
Joined: 31 Jan 2008 Posts: 361 Helped: 17 Location: India
|
24 Apr 2008 5:00 Re: Declaration Vs definition |
|
|
|
| sivamit wrote: |
Hi what is the diff between these two..?
Can anyone please explain..?
Thanks
Shiva |
In Declaration you will just declare the function with its return type, which shows that this function will be used in future. and usually declaration is done before starts the main loop.
In Defination you will define the function including its body and all statements, which does some tasks.
when programme is running the and the function is called, the control jumps to the defination of function and return the value as per its declaration.
|
|
| Back to top |
|
 |
sivamit
Joined: 01 Dec 2005 Posts: 113 Location: India
|
24 Apr 2008 7:35 Re: Declaration Vs definition |
|
|
|
Thanks yousif..
Itz really a great explanation
_Shiva
|
|
| Back to top |
|
 |
sivamit
Joined: 01 Dec 2005 Posts: 113 Location: India
|
24 Apr 2008 10:27 Re: Declaration Vs definition |
|
|
|
| jhbbunch wrote: |
Declaration is like a guy saying to a woman "I will love you forever."
A definition is like the guy saying to the woman "Please marry me." then her accepting the proprosal and the two actually getting married. It is now definite, not just promised. |
Explain the things in software languages ... pls
|
|
| Back to top |
|
 |
saubhik
Joined: 27 Apr 2007 Posts: 108 Helped: 3 Location: Mumbai
|
05 May 2008 6:15 Re: Declaration Vs definition |
|
|
|
| Quote: |
| Variable Definition vs Declaration |
The term declaration rather loosely used when referring to variables. The ``declare''-tion of a variable means that the compiler is told about the variable; i.e. its type and its name, as well as allocated a memory cell for the variable (either locally or globally). This latter action of the compiler, allocation of storage, is more properly called the definition of the variable. The stricter definition of declaration is simply to describe information ``about'' the variable.
Eg. ,
Declaration of a variable : : :
extern int a;
Definition of a variable : : :
int a = 5;
|
|
| Back to top |
|
 |