Explain Binding of a variable in C language.

CServer Side ProgrammingProgramming

Storage classes specify the scope, lifetime and binding of variables.

To fully define a variable, one needs to mention not only its ‘type’ but also its storage class.

A variable name identifies some physical location within computer memory, where a collection of bits are allocated for storing values of variable.

Storage class tells us the following factors

  • Where the variable is stored (in memory or cpu register)?
  • What will be the initial value of variable, if nothing is initialized?
  • What is the scope of variable (where it can be accessed)?
  • What is the life of a variable?

Binding

Binding finds the corresponding binding occurrence (declaration/definition) for an applied occurrence (usage) of an identifier.

  • Scope of variables should be known.

What is the block structure?

In which block the identifier is variable?

  • What will happen if we use same identifier name again?

‘C’ forbids use of same identifier name in the same scope.

Same name can be used in different scopes.

Example

double f,y;
int f( ) //error {
   ---
   ----
   ----
}
double y; //error

Example

double y;
int f( ){
   double f;//legal
   int y; //legal
}

Example

Following is the C program for binding of a variable −

 Live Demo

#include<stdio.h>
int i=33;
main() {
   extern int i; {
      int i=22; {
         const volatile unsigned i=11;
         printf("i=%d\n",i);
      }
      printf("i=%d",i);
   }
}

Output

When the above program is executed, it produces the following output −

i=11
i=22
raja
Published on 25-Mar-2021 08:14:53
Advertisements