Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Static Storage Class

Posted on 2004-03-26
4
Medium Priority
?
405 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
[X]
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
  • 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

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…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
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 opening and reading files in the C programming language.

704 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