Difference between Declaration and Defination

In general definition is where the variable or function is whereas code tell the compiler to allocate some memory and initial values goes to it,whereas declaration is the place where we define its name,type, return type to compiler etc.


For Functions:
//declaration 
int foo(char a,int );
//definition 
int foo(int a , int b)  {
.....
}
For automatic and register  variable definition & declaration are same bcoz they allocate memory  whenever defined.


For external  storage class variables they are different.Global variables are defined in another file.Symols are resolved in time of  object linking.


Externals can be declared and defined outside a function without any storage class(?).They can be declared using the"extern" keyword [e.g. extern int a;]
e.g f1.c
int var_name ;  //declaration only for global and static (Only initialized in BSS but no memory storage)
int var2_name ; //de
int var3_name = 22 ; //declaration and definition
void foo2(int arg1 /*automatic storage,declaration and definition  as allocate mem in stack */)
{
var_name = 15; //definition iff not initialized earlier
}
e.g. in f2.c
extern var2_name = 45; //definition, memory allocation in var2_name,moved to data segment from bss
extern int var_name; //declaration only


Static is default storage class of global variables. So same thing true for local module/object static variables.

Comments

Popular posts from this blog

Airtel Digital tv remote factory reset and reprogram

Tracking Linux kworker threads

Asynchronus I/O (AIO) in vxworks and Linux