Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Static Storage Class

Posted on 2004-03-26
4
390 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:
Avik77 earned 50 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:Avik77
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
 
LVL 9

Expert Comment

by:ankuratvb
ID: 10694004
0

Featured Post

ScreenConnect 6.0 Free Trial

Discover new time-saving features in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

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

Suggested Solutions

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…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand and use pointers 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.

809 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