The static modifier keyword
 

  • The C language provides a static modifier that alters some property of variables

  • A local variable or a global variable can be modified by the static keyword:

     int x;            // Global variable
     static int y;     // Static global variable
    
     int f( )
     {
        int x;         // Local variable
        static int y;  // Static local variable
     }
    

  • The modified variables are called:

      • Static local variables        and         
      • Static global variables

static local variables
 

  • The static modifier applied on a local variable will changes:

      • The life time of a local variable

    (The scope of a static local variable is the same as a local variables)


  • The life time of a static local variable is:

      • Starts when the program begins to run

      • Ends when the program exits

  • Important consequence:

      • A static local variable will only be initialized once

    (because initialization is done when the variable is created/allocated.)

An application of static local variables

Keep a count on the # times that a function was invoked:

   #include <stdio.h>

   void f( )
   {
      static int j = 0;  // Static local 

      j++;
      printf("# times f( ) called: %d\n", j);

      .... // Body of f( ) omitted 
   }

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

DEMO: demo/C/set1/static-local.c

The static local variable j will keep a count on the number of times that f( ) was invoked

static global variables
 

  • The scope of a global variable is modified by the static keyword

  • The scope of a static global variable is:

      • A static global variables can only be accessed by functions defined in the same C program file

    ( In contrast:   global variables can be accessed in all C program files !)


  • Comment:

      • The original C programming language back in 1960's do not have static global variables !

      • The static global variable was introduced later into the C language due to influence of the private classifier in C++/Java that limits the access location to variables

The usage of static global variables
 

Consider: a previous example on global variables in 2 program files:

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;     
 int y = 4;                    

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



   

The main( ) function can access the global variables
DEMO: /home/cs255001/demo/C/set1/static-glob1.c + static-glob2.c

The usage of static global variables
 

When we change the variable x into a static global:

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

 extern int x, y;  

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

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

 // Bugs related to x must be
 // in functions in this file !
 // because x is static global 

The main( ) function cannot access the static variable x !!! (compile error)
DEMO: /home/cs255001/demo/C/set1/static-glob1.c + static-glob2.c

Usage:   static global can limit the search for bugs to the program file that contains the static global variable
Because functions in other program files cannot access the static global variables !