Tuesday 24 May 2016

Scope rule in C Language


Scope Rule in C


Scope of an identifier is the part of the program where the identifier may directly be accessible. In C, all identifiers are lexically (or statically) scope. 
two type of Scope:-
Global Scope: Can be accessed anywhere in a program.
// filename: file1.c
int a;
int main(void)
{
   a = 2;
}
// filename: file2.c
// When this file is linked with file1.c, functions of this file can access a
extern int a;
int myfun()
{
   a = 2;
}
To restrict access to current file only, global variables can be marked as static.
Block Scope: A Block is a set of statements enclosed within left and right braces ({ and } respectively). Blocks may be nested in C (a block may contain other blocks inside it). A variable declared in a block is accessible in the block and all inner blocks of that block, but not accessible outside the block.
What if the inner block itself has one variable with the same name?
If an inner block declares a variable with the same name as the variable declared by the outer block, then the visibility of the outer block variable ends at the pint of declaration by inner block.
int main()
{
  {
      int x = 10, y  = 20;
      {
          // The outer block contains declaration of x and y, so
          // following statement is valid and prints 10 and 20
          printf("x = %d, y = %d\n", x, y);
          {
              // y is declared again, so outer block y is not accessible
              // in this block
              int y = 40;
    
              x++;  // Changes the outer block variable x to 11
              y++;  // Changes this block's variable y to 41
      
              printf("x = %d, y = %d\n", x, y);
          }
          // This statement accesses only outer block's variables
          printf("x = %d, y = %d\n", x, y);
      }
  }
  return 0;
}
Output:
x = 10, y = 20
x = 11, y = 41
x = 11, y = 20

No comments:

Post a Comment