Global variables

Global variables are defined outside all functions:

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

   void f( )
   {
      int y;

      x = y + 3;
   }

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

C's global variables are similar to Java's class (static) variables

Global variables - (lexical) scope

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

   int x;       // Global variable
   int y = 4;   // Global variable              
                                                
   void f( )                                    
   {                                            
      int y = 7;     // Overshadows global var y
                                                
      x = y + 3;  // Uses y = 7                 
   }

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

in any program file starting from the place of definition or variable declaration until the end of the program file --- unless it is overshadowed by a local variable (with the same name)

Global variables - (lexical) scope

Example of scoping error with a global variable:

   int x;       // Global variable


   void f( )
   {
      x = y + 3;  // Use of y outside it's scope
   }

   int y = 4;   // Global variable      // Scope of y

   int main( int argc, char* argv[] )   
   {                                       
      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

    (Because local variables is always defined before it can be "used" (= scoping rule !), there is no need to declare them !!)

Difference between defining a variable and declaring a variable
 

  • A (global) variable definition:

       int  y; // define global variable y
      

    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; // declare global variable y
      

    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 !

Example of when we need to use a variable declaration

A global variable can be defined after it was used:

   #include <stdio.h>

   int x;          // Global variable


   void f( )
   {
      x = y + 3;  // Compile error: y undefined !!
   }

   int y = 4;   // Global variable y (defined too late)  

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

The C compiler needs to know the data type of y when it translates   x = y + 3!!!

Example of when we need to use a variable declaration

Declaring the variable y will solve this compile error:

   #include <stdio.h>

   int x;          // Global variable
   extern int y;   // Declare variable y

   void f( )
   {
      x = y + 3;  // Fixed: y is declared !!
   }

   int y = 4;   // Global variable y   

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

The C compiler knows the data type of y when it translates   x = y + 3!!!

Declarations: making global variables accessible in multiple program files
 

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 progFile1.c: '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 !

Declarations: making global variables accessible in multiple program files
 

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

Postscripts on declarations of global variables

You can have multiple declarations for the same global variable (as long the declarations do not conflict with each other):

   #include <stdio.h>

   int x;     
   extern int y;    // Declare y
   extern int y;    // Declare y again (no conflict)
   void f( )
   {
      x = y + 3;
   }

   int y = 4;   // Global variable 

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

Postscripts on declarations of global variables

Declarations of global variables can be written inside a function:

   #include <stdio.h>

   int x;     

   void f( )
   {

      extern int y;    // Declare y

      x = y + 3;
   }

   int y = 4;   // Global variable 

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

Then the declarations are subjected to the scoping rule of local variables !

Postscripts on declarations of global variables

Example of declaration and scoping rule:

   #include <stdio.h>

   int x;     

   void f( )
   {
      {
      extern int y;    // declaration  
      } 
      x = y + 3; // Error: y is undeclared !
   }

   int y = 4;   // Global variable 

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

The scope of declaration of variable y ended in the inner scope !!