|
|
1.1 root 1:
2:
3: static C Keyword static
4:
5:
6:
7:
8: Declare storage class
9:
10:
11: static is a C storage class. It has two entirely different
12: meanings, depending upon whether it it appears inside or outside
13: a function.
14:
15: Outside a function, static means that the function or variable it
16: preceeds may not be seen outside the module.
17:
18: Inside a function, static may only precede a variable. It means
19: that that variable is permanently allocated, rather than
20: allocated on the stack when the function is entered and discarded
21: when the function exits. If a static variable is initialized,
22: that occurs before the program starts rather than every time the
23: function is entered. If a function returns a pointer to a vari-
24: able, often that variable is declared static within the function.
25: If a pointer to a nnoonn-ssttaattiicc local variable is returned, that
26: variable is freed when the function returns and the pointer
27: points to an unprotected location.
28:
29: ***** Example *****
30:
31: The following example demonstrates the uses of the static
32: keyword. It returns the next integer in a sequence as a string.
33:
34:
35: /* static to keep function hidden outside of this module */
36: static char *nextInt()
37: {
38: /* static to protect value between calls */
39: static int next = 0;
40: /* static to allow the return of a pointer to s */
41: static char s[5];
42:
43:
44:
45: sprintf(s, "%d", next++);
46: return(s);
47: }
48:
49:
50: ***** See Also *****
51:
52: auto, C keywords, extern, register variable, storage class
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64: COHERENT Lexicon Page 1
65:
66:
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.