Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Static Storage Class

Posted on 2004-03-26
4
Medium Priority
?
406 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
ID: 10693490
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:
Avik Dasgupta earned 200 total points
ID: 10693913
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:Avik Dasgupta
ID: 10693931
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.

926 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