Local variables
 

When a variable is defined inside some function:

   #include <stdio.h>

   int main( int argc, char* argv[] )            
   {
      int x;       // Local variable
      int y = 4;   // Local variable

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

the variable is a local variable

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 "x" refers to the given variable

Important scoping restriction

  • Every variable name must be unique inside a scope

    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/tmp/demo.c

Lexical scoping rule for local variables in the C language

  • The lexical scope (or scope) of a local variable in a C function 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          

  • starts from the place of definition of the local variable
  • continues into any inner scopes
  • stops at the end of the scope that contains the local variable definition
  • unless it has be overshadowed by a variable in an inner scope

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   
                                               
    }  
                                  
 }                  
   

One variable x=4 is in the outer scope and another variable x=7 is in the inner scope

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[] )            
 { // Outer scope

    int x;     


    {  // Inner scope
       int y = 4;               

       x = y + 3;
    }                           
    printf( "%d  %d\n", x, 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 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 forbids using the same variable name in an inner scope !!!

Local variables - (lexical) scope

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 algorithm

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

Local variables - life time
 

The life time of a local variable is:

   #include <stdio.h>

   int main( int argc, char* argv[] )            
   {                             
      int x;       // Local variable
      int y = 4;   // Local variable

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

From the time when the function starts running till the time when the function returns

Remember from assembler programming: local variables are created on the runtime stack !!!

Scope and life time 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 and life time of parameter variables ?

Scope and life time of parameter variables

The scope and life time of parameter variables are 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;

     ...




  }
  

And we have just discussed the scope and life time of local variables...