.TH static "" "" "C Keyword"
.PC "Declare storage class"
.PP
.B static
is a C storage class.
It has two entirely different meanings, depending upon whether it
appears inside or outside a function.
.PP
Outside a function,
.B static
means that the function or variable it preceeds
may not be seen outside the module.
.PP
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
.B 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 variable,
often that variable is declared
.B static
within the function.
If a pointer to a
\fBnon-\fBstatic\fR local variable is returned, that variable is
freed when the function returns and the pointer points to an
unprotected location.
.SH Example
The following example demonstrates the uses of the
.B static
keyword.
It returns the next integer in a sequence as a string.
.DM
/* 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];    
.DE
.DM
	sprintf(s, "%d", next++);
	return(s);
}
.DE
.SH "See Also"
.Xr "auto," auto
.Xr "C keywords," c_keyword
.Xr "extern," extern
.Xr "register variable," registerv
.Xr "storage class" storage_c
.br
\*(AS, \(sc6.5.1
