Scope and Extent in C Programming Part-1



Scope and Extent
                The Scope of a variable refers to the extent to which different parts of a program have access to the variable in other words, Where the variable is visible. Scope also effect a variable’s lifetime i.e. how long the variable persists in memory, or in other words when the variable’s storage is allocated and deallocated.





Type of Scope:
There are four types of scope:
1.  Block Scope: Block scope signifies that the variable is active from its declaration point to the end of the block in which it is declared. A block is any series of statement enclosed in braces(). This includes compound statements as well as function’s bodies.
2.  Function Scope: Function scope signifies that the variable is active from the beginning to the end of the function.
3.  File Scope:  File scope signifies that the variable is active from its declaration point to the end of the source file.
4.  Program Scope: Program scope signifies that the variable is active among different source file that make up the entire executable program. Variables with program scope are often referred to as global variables.
            In general, the scope of a variable is determined by the location of its declaration variables declared within a block have block scope. The variables declared outside of a block have file scope if the static keyword is present, or program scope if static is not present. The goto labels have scope within the function.
The program fragment shown below contains variables with all the four types of scope:
int  I;                 /* Program scope */
static  int  j;      /* File scope */
func (k)             /* Program scope */
int  k;                /* Block scope */
{   int  m;          /* Block scope */
Start :              /* Function scope */
----------
----------   }





                  Programming language C allows you to give two variables the same name, provided they have different scope. For example, the two functions shown below, use a variable called j, but because they are declared in different blocks, they do not conflict, with one another’s values:
function1 ()
{  int  j;
--------
-------- }
function2 ()
{   int  j;
--------
--------  }
              It is also possible for variables with the same name to have different scopes that overlap. In such a case, the variable with the smaller scope temporarily “hides” the other variables. For example:
int  j = 10;       /* program scope */
main ()
{  int  j;          /* Block scope- hides global  j  */
for (j = 0; j < 5; ++j)
   printf ( “j : %d”, j );   }
            There are two j’s, one with a value  j= 10 has program scope. The other j is used with block scope. Although they have the same name, they are distinct variables. The j with block scope temporarily hides the j with program scope, sp the result of running the program is:
j : 0
j : 1
j : 2
j : 3
j : 4
Note that the j with program scope retains its value i.e. 10.
Scope and Extent in C Programming Part-1 Scope and Extent in C Programming Part-1 Reviewed by vijay pratap singh on June 23, 2017 Rating: 5

No comments:

Powered by Blogger.