summaryrefslogtreecommitdiff
path: root/reference/C/SYNTAX/static.htm
blob: 273eb3a5de17d117a3e5707b37005e9a3ec50b1c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
<title>static functions</title>

<head>
<script language="JavaScript">
</script>
</head>

<body bgcolor="#ffffcc">
<hr>
<center>
<h1>static functions</h1>
</center>
<hr>
<p>

<b>static functions</b> are functions that are only visable to other
functions in the same file. Consider the following code.
<p>
<table border=2 bgcolor=ivory align=left>
<th>
main.c
</th>
<tr>
<td>
<pre>

   #include <stdio.h>

   main()
   {
     Func1();   

     Func2();
   }
   
</pre>
</td>
</tr>
</table>

<table border=2 bgcolor=ivory>
<th>
funcs.c
</th>
<tr>
<td>
<pre>

   /*************************************
    *
    * Function declarations (prototypes).
    *
    *************************************/

   /* Func1 is only visable to functions in this file. */   

   <font color=red><i>static</i></font> void Func1(void);

   /* Func2 is visable to all functions. */

   void Func2(void); 

   /*************************************
    *
    * Function definitions
    *
    *************************************/
       
   void Func1(void)
   {
     puts("Func1 called");
   }
   
   /*************************************/
   
   void Func2(void)        
   {
     puts("Func2 called");
   }
   
</pre>
</td>
</tr>
</table>
<p>
If you attempted to compile this code with the following
command,
<p>

<center>
<table border=2 bgcolor=ivory>
<tr>
<td>
<pre>

   gcc main.c funcs.c   
</pre>
</td>
</tr>
</table>
</center>
<p>
it will fail with an error simular to.....
<p>

<center>
<table border=2 bgcolor=ivory>
<tr>
<td>
<pre>

   undefined reference to `Func1'  
</pre>
</td>
</tr>
</table>
</center>
<p>
Because 'Func1' is declared as <font color=red><i>static</i></font>
and cannot be 'seen' by 'main.c'.
<p>
<hr>
<h2>Notes:</h2>
For some reason, <font color=red><i>static</i></font> has different
meanings in in different contexts.
<ol>
<li> When specified on a function declaration, it makes the
function local to the file.
<p>
<li> When specified with a variable inside a function, it
allows the vairable to retain its value between calls to
the function. See
<a href=../CONCEPT/storage_class.html#static>static variables</a>.
</ol>
It seems a little strange that the same keyword has such
different meanings.... 

<p>
<hr>
<h2>See Also:</h2>

<img src="../../GRAPHICS/whiteball.gif">
<a href="../CONCEPT/storage_class.html#static">static variables</a><p>

<font color=brown>
<img src="../../GRAPHICS/whiteball.gif">
<a href="../../CPLUSPLUS/SYNTAX/static.htm">C++ extensions for </a>static<p>
</font>


<p>

<hr>
<p>
<center>
<table border=2 width="80%" bgcolor="ivory">
<tr align=center>
<td width="25%">
<a href="../cref.html">                  Top</a>
</td><td width="25%">
<a href="../master_index.html">          Master Index</a>
</td><td width="25%">
<a href="keywords.html">                 Keywords</a>
</td><td width="25%">
<a href="../FUNCTIONS/funcref.htm">      Functions</a>
</td>
</tr>
</table>
</center>
<p>
<hr>
<address>Martin Leslie 
</address><p>
</body>
</html>