Sunday, June 26, 2016

Variable & Function Declaration and Defintion Explanied


  • 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, these two operations may occur independently. This is important because memory for a variable must be allocated only once, to ensure that access to the variable always refers to the same cell. Thus, 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.


Variable Declaration Vs Definition Differentiation Parameters

A. Space Reservation :

  1. Whenever we declare a variable then space will not be reserved for the variable.
  2. Whenever we declare a variable then compiler will not look for other details such as definition of the variable.
  3. Declaration is handy way to write code in which actual memory is not allocated.
struct book  {
    int pages;
    float price;
    char *bname;
};
In the above declaration memory is not allocated. Whenever we define a variable then memory will be allocated for the variable.
struct book b1;



No comments:

Post a Comment