C Programming language

adplus-dvertising
C Programming Language: Difference between Definition and Declaration
Previous Home Next

Both objects and functions can be defined and declared. When we declare a variable or object then its attribute is known to the compiler. When we define a object then the attribute is known to the compiler and the object is also be created. Definition means where a variable or function is defined and memory is allocated for variable or function.

Whenever a variable is defined within the function then it has local scope (local scope means that variable con only access locally). While when a variable is defined outside the function then it has global scope (global scope means we can access the variable globally or anywhere).

Example of definition and declaration :

void fun(int num)
{
	int n;   // Declaration of variable
	n= num; // Definition of variable 
}

Here we are passing integer type parameter to fun function and we are also declaring n as integer which is known by compiler that is called Declaration and are assigning the value num to n is called definition.

Example for Global Scope of Variable :

int  num;
char letter;  // int num and char are global variables
main()
{
	num=23;
	letter=’A’;
}

Example for Local Scope of Variable :

main()
{
	int num;  // int num and char ch are local variables        
	char ch;
	num=2;
	ch=’a’;
}
Previous Home Next