Friday 12 April 2013

Declaration and Definition of variables and functions

The main difference declaration and definition of variables and functions is that when we declare a variable or function then there is no memory allocated for that variable and function .If we declare a variable and put some value in it along with then it is called definition .We can understand this by following examples:-

Functions:-

void some_function(void); 
//function declaration because there is no memory created for it because by default it is extern outside the function

extern void some_function(void);
//function declaration because it is declared as extern it is always be a declaration because there is no memory allocated for it either will be in function or outside the function

some_function()
{
..................//this is a definition because defintion created for it
}
============
Variables:-

int i;        
//definition because there is 4-byte memory allocated for it and some garbage value put into it.

int i=0;
//definition because there is 4-byte memory allocated for it and zero put into it.

int i=5;
//definition because there is 4-byte memory allocated for it and 5 put into it.

extern int i;
//declaration because there is no memory allocated for it.

extern int i=5;
//definition because there is 4-byte memory allocated for it and 5 put into it 

No comments:

Post a Comment