C Language and Libraries Help (clang.hlp) (Table of Contents; Topic list)
Important Notice
The pages on this site contain documentation for very old MS-DOS software, purely for historical purposes. If you're looking for up-to-date documentation, particularly for programming, you should not rely on the information found here, as it will be woefully out of date.
static
                                             Up Contents Index Back
─────C/C++ Language─────────────────────────────────────────────────────────
 
  Keyword:   static
 
  Syntax:    static declarator
 
  Summary:   When modifying a variable, 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, 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,
             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, specifies that the function
             accesses only static members.
 
  See also:  auto, extern, register
 
  Example:
 
      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
                                 //    through multiple invocations of
                                 //    the function
 
          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;
                                    -♦-