Difference between Definition and Declaration in Coding
Both are very confusing terms in the world of coding. These two concepts are different to each other. The term Definition is used in the memory assignment of a variable while the declaration memory is not allocated to any variable.
What is Definition?
The definition is a term used to identify the code associated with class, variable, function etc. A compiler must be needed to use the definition. When a variable is defined, then the definition holds some memory. It provides the code for any function. The definition of a program is unique; that's why it can be used only once in a program. There are so many connections between declaration and definition. Sometimes we cannot define a variable in the program, but we can determine that variable in that program.
For example, we cannot use the class definition, but we can declare it.
Definition Example:
- Variable definition as well as the declaration:
int p = 11;
- Function definition:
nt add (int v, int k)
{
int g;
g = v + k;
return g;
}
What is Declaration?
We use the term declaration for specifying a name in the program like variable, class, function, variable and expression function etc. We cannot use any name without declaring it in the code. So first, we have to report the name in the code, and then we can use it in the code. We can declare an element many times in the code. We can also use the declaration statement to increase the visibility of the code.
There are other reasons to use the definition statement instead of the declaration in the code. These are as the following-
- When the declaration statement becomes a typed statement.
- When the class name is declared without defining it.
- When we want to declare an external link to the code.
- When we want to declare a static variable inside the code, we first have to define that variable in the code; after that, we can claim that in the code.
Declaration Example
- Declaration of variable:
extern int h;
- Declaration of function:
int add (int a1, int a2);
Definition | Declaration |
It is used to determine the value like class, variable etc., used in the code or program. | It specifies the name, like class, variable in the program or code. |
Memory allocation occurs in the case of definition. int car(int ship) { // some code return ship; } int shop; This is a definition of variables and functions. The allocation of storage is present here. | The memory allocation does not take place in the case of declaration. int more(int); extern int shop; This is a declaration of variables and functions. This declaration is used for informing the compiler about the variable and function. |
The statement cannot be declared in the code or program more than once. | In the case of Declaration, Redeclaration is easily possible in the code or program. |
Its duration is determined. | Its visibility is specified. |
It is used where the identifier is implemented. | It introduces an identifier, and then describes it. |
The identifier is used to complete the linking process with the compiler in the code. For example int main() { extern int more; printf("%d\n", more); return 0; } | To allow the identifier for reference, compiler needs to identify it. For example int main() { printf("%d\n", main); return 0; } |
A definition can be used instead of declaration. | But a declaration cannot use instead of definition. |
A variable can be defined only once in the code. int main() { int more; int more; int more; return 0; } | A variable or a function can be declared any number of times. For example, int more; int main() { extern int more; extern int more; extern int more; printf("%d", more); return 0; } |
{ int more; int more; int more; return 0; } | int main() { extern int more; extern int more; extern int more; printf("%d", more); return 0; } |
Conclusion
A variable can be defined and declared in the case of C language. It is also possible to define and declare the function. The main difference between definition and declaration is a declaration of a variable indicates the compiler, the name and the type of the variable. In contrast, the definition of a variable tells the compiler where and how much storage to create for a variable.