Solved

Static Storage Class

Posted on 2004-03-26
4
357 Views
Last Modified: 2010-04-15
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
Comment
Question by:Wtwh
  • 2
4 Comments
 
LVL 14

Expert Comment

by:Farzad Akbarnejad
Comment Utility
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
 
LVL 2

Accepted Solution

by:
Avik77 earned 50 total points
Comment Utility
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
 
LVL 2

Expert Comment

by:Avik77
Comment Utility
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
 
LVL 9

Expert Comment

by:ankuratvb
Comment Utility
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now