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

Local variables - (lexical) scope
 

The lexical scope (or scope) 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\n", x, y );
   }                             
   

starting from the place of definition till the end of the scope that contains the local variable definition

Local variables - (lexical) scope
 

The Example of scoping error:

   #include <stdio.h>

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

      x = y + 3;

      int y = 4;   // Local variable

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

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

Local variables - (lexical) scope
 

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

  • A local variable can only be used (= accessed) inside the function in which the local variable has been defined
 

Purpose of local variables:

  • Local variables are used to store short term information to help the porgrammer 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 !!!

Global variables
 

When a variable is defined outside all functions:

   #include <stdio.h>

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

   void f( )
   {
      x = y + 3;
   }

   int main( int argc, char* argv[] )            
   {
      f( );
      printf( "%d  %d\n", x, y );
   } 
   

the variable is a global variable

Global variables - (lexical) scope

The lexical scope (or scope) of a global variable is:

   #include <stdio.h>

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

   void f( )
   {
      x = y + 3;
   }

   int main( int argc, char* argv[] )            
   {
      f( );
      printf( "%d  %d\n", x, y );
   } 
                                       

in any program file starting from the place of definition or variable declaration till the end of the program file

Global variables - (lexical) scope

Example of scoping error with a global variable:

   #include <stdio.h>

   int x;       // Global variable


   void f( )
   {
      x = y + 3;
   }

   int y = 4;   // Global variable 

   int main( int argc, char* argv[] )            
   {
      f( );
      printf( "%d  %d\n", x, y );
   } 
                                       

When  y was used, the variable definition was not yet processed !
Remember that the C-compiler is 1-pass !!!

Declaring a global variable
 

  • The C language has an feature that is not found in the Java language:

      • Declaring global variables       

  • Syntax to declare a global variable:

         extern  dataType  globalVarName;      
      

    Note: local variables cannot be declared !!

Difference between defining a variable and declaring a variable
 

  • A (global) variable definition:

          int  y; // global variable
      

    will:

      1. convey the data type information to the C compiler

      2. tell the C compiler to reserve memory space in the .data segment for the variable

        (I.e.: output y: .skip 4 in the .data segment)

Difference between defining a variable and declaring a variable
 

  • A (global) variable declaration:

          extern int  y; // global variable
      

    will:

      1. Only convey the data type information to the C compiler

        Declarations provide type information to the C compiler to enable it to translate the statement correctly !!!

        Note: the variable definition will (must) be specified elsewhere in the C program !

Why do we need the variable declaration ?

We have discussed this in the following webpage: click here

   #include <stdio.h>

   int x;       // Global variable


   void f( )
   {
      x = y + 3;  // Compiler needs type info on y !!
   }

   int y = 4;   // Global variable 

   int main( int argc, char* argv[] )            
   {
      f( );
      printf( "%d  %d\n", x, y );
   } 
   

 

Global variables - (lexical) scope of a declaration !

Example of variable declaration (only use with global variables):

   #include <stdio.h>

   int x;     
   extern int y;    // Declaring a variable
                    // Starts the scope of variable y ! 
   void f( )
   {
      x = y + 3;
   }

   int y = 4;   // Global variable 

   int main( int argc, char* argv[] )            
   {
      f( );
      printf( "%d  %d\n", x, y );
   } 
                                       

When  y was used, the variable declaration was processed - no errors !

Global variables - (lexical) scope
 

Another example of scoping error with a global variable:

progFile1.c progFile2.c
 #include <stdio.h>

 int main( int argc, char* argv[] ) 
 {
    f( );
    printf( "%d  %d\n", x, y );
 } 
   
 int x;       // Global variable
 int y = 4;   // Global variable 

 int f( )
 {
    x = y + 3;
 }
   

You will get this error when you compile: 'x' undeclared and 'y' undeclared

The C compiler can not process the program file progFile1.c because the data type of x and y are unknown !

Global variables - (lexical) scope
 

Fix with declarations:

progFile1.c progFile2.c
 #include <stdio.h>

 extern int x, y;  

 int main( int argc, char* argv[] ) 
 {
    f( );
    printf( "%d  %d\n", x, y );
 } 
   
 int x;       // Global variable
 int y = 4;   // Global variable 

 int f( )
 {
    x = y + 3;
 }
   

DEMO:   /home/cs255001/demo/tmp/demo.c + demo2.c

Apply the scoping rule on x and y to understand that the declaration makes this program correct

Global variables - life time

The life time of a global variable is:

   #include <stdio.h>

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

   void f( )
   {
      x = y + 3;
   }

   int main( int argc, char* argv[] )            
   {
      f( );
      printf( "%d  %d\n", x, y );
   } 
   

Global variables are "permanent" variables

Their life time is the entire duration of the program execution