Tuesday, July 16, 2013

The static keyword in C/C++ programming


I will use a very simple project made by 2 files: main.c and extra.h  and their contents are reported below:


/* ****** extra.h ************* */

#ifndef EXTRA_H
#define EXTRA_H



/* Declaration of the static variable with file scope */
static int FileVariable=100;

void SetToOne()
{
    FileVariable=1;
}

void SetToTwo()
{
    FileVariable=2;
}

int GetValue()
{
    return FileVariable;
}







int CheckCounter()
{

     /* Declaration of the static variable within the function body */
    static int nCounter=0;
    nCounter++;
    return nCounter;   
}

#endif




/* *** *** *** main.c *** *** *** */


#include <stdio.h>
#include "extra.h"

int main()
{
    printf("Testing the static keyword... \n");
   
    SetToOne();
    printf("FileVariable is %d \n", GetValue());
 
    SetToTwo();
    printf("FileVariable is %d \n", GetValue());
   
    IncreaseCounter();
    IncreaseCounter();

    printf("Counter is %d \n", IncreaseCounter());


    return 0;
}


On Linux/Mac OS X, to compile the program with gcc and run it, use the command:

cc -o main.c; ./a.out



If you made everything properly, the output will be:

FileVariable is 1
FileVariable is 2
nCounter is 3


What happened?
In C/C++, the static keyword has two basic uses.

1) To limit the visibility of a function or variable to the current file. 
This is the case for the variable FileVariable which is visible only to  the functions defined in extra.h, i.e. SetToOne, SetToTwo, GetValue and IncreaseCounter.
Just to be clearer, if you already know what global varibles are, well, static variables are just like global variables but only within the file they are defined.

If you attept to to do use or modify FileVariable within the main.c, you'll get an ERROR!!!
Try it!

FileVariable=100;  /* does not work! */

The only way to allow the previous expression is to remove the keyword static. Doing so,  FileVariable becomes a global variable and you can modify it freely both in main.c and extra.h



2) To let a variable to survive even if the function having it terminates.
Is it clear to you why nCounter is 3?
Within main.c, we called function 3 times! Check it out!
Without the keyword static, the variable Counter would have been created and re-set to zero at each function call.
Try it!
Remove the keyword static  and re-run the program.



NOTE:
In C++ the static keyword can also be used within a class, as  a counter. In this case, the static variable must be public and must be initialized outside the class.

class MyClass
{
    private:
    int    m_nVal;  // just one integer variable
    float m_fVal; // just one float variable

   public:
   MyClass(int nVal, float fVal)   // constructor for this class
  {
     m_nVal=nVal;
     m_fVal=fVal;
  }

  static int Counter;  // Our  static counter

};

// Initialization of the static variable...
static MyClass::Counter=0;



No comments:

Post a Comment

Your comment will be visible after approval.