[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 409
  • Last Modified:

Static Storage Class

I have a simple enquiry. I am going through some C codes and I need to find out what is the use of declaring variables as "static".

Although, I have the definition of the static storage class but I cant appreciate why we need to explicitly declare variables as "static" and what would happen if we don't.

Would appreciate any one who can give examples of a application to illustrate why it is neccessary to declare a variable as "static"

Thank you.


0
Wtwh
Asked:
Wtwh
  • 2
1 Solution
 
Farzad AkbarnejadDeveloperCommented:
Hi,
Quote from MSDN:

static
static declarator

When modifying a variable, the static keyword specifies that the variable has static duration (it is allocated when the program begins and deallocated when the program ends) and initializes it to 0 unless another value is specified. When modifying a variable or function at file scope, the static keyword specifies that the variable or function has internal linkage (its name is not visible from outside the file in which it is declared).

In C++, when modifying a data member in a class declaration, the static keyword specifies that one copy of the member is shared by all the instances of the class. When modifying a member function in a class declaration, the static keyword specifies that the function accesses only static members.

For related information, see auto, extern, and register.

Example

// Example of the static keyword
static int i;         // Variable accessible only from this file

static void func();   // Function accessible only from this file

int max_so_far( int curr )
{
   static int biggest;    // Variable whose value is retained
                          //    between each function call
   if( curr > biggest )
      biggest = curr;

   return biggest;
}

// C++ only

class SavingsAccount
{
public:
   static void setInterest( float newValue )  // Member function
      { currentRate = newValue; }             //    that accesses
                                              //    only static
                                              //    members
private:
   char name[30];
   float total;
   static float currentRate;    // One copy of this member is
                                //    shared among all instances
                                //    of SavingsAccount
};

// Static data members must be initialized at file scope, even
//    if private.
float SavingsAccount::currentRate = 0.00154;


-FA
0
 
Avik DasguptaCommented:
A simple example
void f();
void main()
{
 f(); // call first time
 f(); // call second time
}

void f(){
 static int a=1;
 int b=1;
 printf("%d  %d",a,b);
 a++; b++;
}
output will be
1    1
2    1

the lifetime of static variable is throughout the execution period
but the lifetime of automatic or other classes are restricted only within its scope.
Hence in any application where u want to preserve the lifetime of a variable only within its scope through out the execution period the static storage class is used.
0
 
Avik DasguptaCommented:
Storage class            LifeTime    
-----------------------------------------------------
Automatic :         dies when out of scope.

Extern  :               same

Register:              same

Static :                throughout the execution of the program.
                          and hence the value is preserved.

Do not confuse urself from global variable. It's scope is global but a static variable declared in a module has a scope within its module, but preserves value when goes outside its scope. During initialization a static variable is initialized to zero.

Avik.

0

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now