

static                      C Keyword                      static




Declare storage class


static  is a  C  storage class.   It has  two entirely  different
meanings, depending upon  whether it it appears inside or outside
a function.

Outside a function, static means that the function or variable it
preceeds may not be seen outside the module.

Inside a function, static  may only precede a variable.  It means
that  that   variable  is  permanently   allocated,  rather  than
allocated on the stack when the function is entered and discarded
when the  function exits.  If  a static variable  is initialized,
that occurs before the  program starts rather than every time the
function is entered.  If a  function returns a pointer to a vari-
able, often that variable is declared static within the function.
If a  pointer to  a nnoonn-ssttaattiicc  local variable is  returned, that
variable  is freed  when  the function  returns  and the  pointer
points to an unprotected location.

***** Example *****

The  following  example  demonstrates  the  uses  of  the  static
keyword.  It returns the next integer in a sequence as a string.


/* static to keep function hidden outside of this module */
static char *nextInt()
{
        /* static to protect value between calls */
        static int next = 0;
        /* static to allow the return of a pointer to s */
        static char s[5];



        sprintf(s, "%d", next++);
        return(s);
}


***** See Also *****

auto, C keywords, extern, register variable, storage class











COHERENT Lexicon                                           Page 1


