Review: scope
 

  • A new scope is   started by {   and   ended by }

    Example:

        int f(int a, int b)           
        { // Scope 1 (outer scope)    
                                      
           { // Scope 2 (inner scope) 
                                      
           }                          
                                      
        }                             
      

  • The lexical scope of a variable name "x":

      • the region of the program where that name (identifier) "x" refers to a given variable

Local variables
 

A variable that is defined inside some scope is a local variable:

   #include <stdio.h>

   int main( int argc, char* argv[] )            
   {  // Start of a scope

      int x;       // Local variable
      int y = 4;   // Local variable

      x = y + 3;
      printf( "%d  %d\n", x, y );
   } 
   

Important scoping restriction

  • Program language rule:

    • Every variable name must be unique inside a scope

  • I.e.:   2 variables with the same name inside the same scope is not allowed:

     int main( int argc, char* argv[] )            
     { // Scope 1
                                         
        int x;     
                          
        {  // Scope 2
           int x; // OK. Only 1 variable of name x in scope 2  
                 
        }           
                                           
        float x;  // Error: x is not unique in scope 1 
     }      
    

DEMO:   /home/cs255001/demo/C/set1/scoping-err1.c (gcc -c scoping-err1.c)

Lexical scoping rule for local variables in the C language

  • The lexical scope (or scope) of a local variable in C is:

     { // Scope 1
                                         
        int x; // Scope of x starts here   
                                                
        {  // Scope 2                    
                                         
           int x; // x is overshadowed by x here   
                                                   
        } //  x stops being overshadowed by x here  
                                            
     } // Scope of x starts here          

    • The scope starts at the place of definition of the local variable

    • continues into any inner scopes
      • unless it has be overshadowed by a variable in an inner scope

    • ends at the end of the scope (}) that contains the local variable definition

I will "unpack" this statement with some examples

Local variables - (lexical) scope
 

Consider the following program with 2 variables with name x:

 #include <stdio.h>

 int main( int argc, char* argv[] )  
 { 

    int x = 4; // Scope of x starts here 


    {                
                                     
       int x = 7; // x is overshadowed by x here   
                                               
    }  
                                  
 }                  
   

(1) variable x=4 is in the outer scope and (2) variable x=7 is in the inner scope

DEMO: demo/C/set1/scoping-demo.c

Local variables - (lexical) scope
 

Example of scoping:

 #include <stdio.h>

 int main( int argc, char* argv[] )  
 { 

    int x = 4; // Scope of x starts here 

    printf("%d\n", x);  // prints 4
    {                
                                     
       int x = 7; // x is overshadowed by x here   
                                               
    }  
                                  
 }                  
   

The variable x in   printf("%d\n", x);   refers to   int x = 4;

Local variables - (lexical) scope
 

Example of scoping:

 #include <stdio.h>

 int main( int argc, char* argv[] )  
 { 

    int x = 4; // Scope of x starts here 


    {         
       printf("%d\n", x);  // prints 4  
       int x = 7; // x is overshadowed by x here   
                                               
    } 
                                   
 }                  
   

The variable x in   printf("%d\n", x);   refers to   int x = 4;

Local variables - (lexical) scope
 

Example of scoping:

 #include <stdio.h>

 int main( int argc, char* argv[] )  
 { 

    int x = 4; // Scope of x starts here 


    {    
         
       int x = 7; // x is overshadowed by x here
       printf("%d\n", x);  // prints 7 !!!
    } 
                                   
 }                  
   

The variable x in   printf("%d\n", x);   refers to   int x = 7;

Local variables - (lexical) scope
 

Example of scoping:

 #include <stdio.h>

 int main( int argc, char* argv[] )  
 { 

    int x = 4; // Scope of x starts here 


    {    
         
       int x = 7; // x is overshadowed by x here    
                   
    }  // End of overshadowing !          
    printf("%d\n", x);  // prints 4  
 }                  
   

The variable x in   printf("%d\n", x);   refers to   int x = 4;

Local variables - (lexical) scope
 

Example of scoping error:

 #include <stdio.h>

 int main( int argc, char* argv[] )  
 { 
    printf("%d\n", x); // Error: x undefined
    int x = 4; // Scope of x starts here 


    {                
                                     
       int x = 7; // x is overshadowed by x here   
                                               
    }      
                              
 }                  
   

The variable  x was used before the start of the scope of the variable x !

Local variables - (lexical) scope
 

Example of scoping error:

 #include <stdio.h>

 int main( int argc, char* argv[] )            
 { 

    int x;     


    { 
       int y = 4;               


    }                           
    printf( "%d\n",  y );  // Error
 }   
 

The variable  y was used after the end of the scope of the variable y !

Difference between C and Java

  • In C, the same variable name defined in an inner scope will overshadow the variable name in the outer scope:

       { // Outer scope
      
          int x = 4;     
      
          {  // Inner scope
             printf( "%d\n", x); // prints 4
      
             int x = 7;  // Overshadows outer x            
      
             printf( "%d\n", x); // prints 7
          } // End of inner x's scope 
      
          printf( "%d\n", x); // prints 4
       }            
      

  • Java disallows using the same variable name in an inner scope !!!

DEMO: demo/C/set1/scoping1.c + scoping1.java

Summary: (lexical) scope of local variables

Where can you use (= access) a local variable in C:

  1. A local variable can be accessed after its definition until the end of its scope

  2. A local variable can be accessed in the inner scopes unless it is overshadowed (by another variable of the same name)
 

Purpose of local variables:

  • Local variables are used to store short term information to help the programmer write the function

  • The localized accessibility of local variables is designed to help the programmer limit the search area to find bugs

The scope of parameter variables

Parameter variables are defined in the function header:

  void funcName( int param1, float param2 )
  {


     int localVar1;
     float localVar2;

     ...




  }
  

What is the scope of parameter variables ?

The scope of parameter variables

The scope of parameter variables is same as local variables that are defined at the start of the function:

  void funcName( int param1, float param2 )
  {
     int param1;   
     float param2  
     int localVar1;
     float localVar2;

     ...




  }
  

Note:   parameter variables are "real" variables and they are created when the arguments are passed (= copied)