summaryrefslogtreecommitdiff
path: root/reference/C/SYNTAX/glo_int_vars.html
blob: 51991c490bcade89a13d6f31604c97ffc50e77da (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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
<title>Global and Local variables declaration.</title>
<head>
<script language="JavaScript">
</script>
</head>
<body bgcolor="#ffffcc">
<hr>
<center>
<h1>Global and Local variables.</h1>
</center>
<hr>
<p>

<h2>Local variables</h2>
Local variables must always be 
<a href="../glossary.html#definition">defined</a>
at the top of a block.
<p>
<font color="brown">
C++ has changed the rules regarding where you can define a
local variable. 
<a href=../../CPLUSPLUS/CONCEPT/local_var.html>Click here</a>
for the low down.
</font>
<p>
When a local variable is 
<a href="../glossary.html#definition">defined</a> 
- it is not initalised by the system, you
must initalise it yourself.<p>
A local variable is 
<a href="../glossary.html#definition">defined</a>
 inside a <b>block</b> and is only visable 
from within the block.
<p>
<center>
<table border=1 width="80%" bgcolor=ivory>
<tr>
<td>
<pre>

   main()
   {
       int i=4;
       i++;
   }

</pre>
</td>
</tr>
</table>
</center>
<p>
When execution of the block starts the variable is
available, and when the block ends the variable 'dies'.<p>
A local variable is visible within nested blocks unless a variable with 
the same name is 
<a href="../glossary.html#definition">defined</a>
 within the nested block.
<p>
<center>
<table border=1 bgcolor=ivory width="80%">
<tr>
<td>
<pre>

   main()
   {
       int i=4;
       int j=10;
       
       i++;
      
       if (j > 0)
       {
          printf("i is %d\n",i);      /* i defined in 'main' can be seen      */
       }
     
       if (j > 0)
       {
          int i=100;                  /* 'i' is defined and so local to 
                                       * this block */
          printf("i is %d\n",i);      
       }                              /* 'i' (value 100) dies here            */
     
       printf("i is %d\n",i);         /* 'i' (value 5) is now visable.        */
   }

</pre>
</td>
</tr>
</table>
</center>
<p>
<a name="global">
<hr width="50%" align=center>
<h2>Global variables</h2>

<font color="brown">
C++ has enhanced the use of 
<a href=../../CPLUSPLUS/CONCEPT/scope.html> global variables.</a>
</font>
<p>
Global variables ARE initalised by the system when you 
<a href="../glossary.html#definition">define</a>
 them!
<p>
<center>
<table border=2 width="50%" bgcolor=ivory>
<tr align=center><td> Data Type </td><td>  Initialser </td> </tr>
<tr align=center><td>   int     </td><td>  0    </td> </tr>
<tr align=center><td>   char    </td><td>  '\0' </td> </tr>
<tr align=center><td>   float   </td><td>  0    </td> </tr>
<tr align=center><td>   pointer </td><td>  NULL </td> </tr>

</table>
</center>
<p>
In the next example <b>i</b> is a global variable, it can be seen and modified by
<b> main </b>and any other functions that may reference it. 
<p>
<center>
<table border=1 width="80%" bgcolor=ivory>
<tr>
<td>
<pre>

   int i=4;
   
   main()
   {
       i++;
   }

</pre>
</td>
</tr>
</table>
</center>
<p>

Now, this example has <b>global</b> and <b>Internal</b> variables.
<p>
<center>
<table border=1 width="80%" bgcolor=ivory>
<tr>
<td>
<pre>

   int i=4;          /* Global definition   */
   
   main()
   {
       i++;          /* global variable     */
       func
   }

   func()
   {
       int i=10;     /* Internal declaration */
       i++;          /* Internal variable    */
   }

</pre>
</td>
</tr>
</table>
</center>
<p>

<b>i</b> in <b>main</b> is global and will be incremented to 5. <b>i</b> in
<b>func</b> is internal and will be incremented to 11. When control returns
to <b>main</b> the internal variable will die and and any reference to
<b>i</b> will
be to the global.<p>


static variables can be 'seen' within all functions in this source file. At
link time, the static variables defined here will not be seen by the object 
modules that are brought in.<p>
<hr>
<h2>Example:</h2>
<img src="../../GRAPHICS/computer.gif">
<a href="../EXAMPLES/global.c">An Example program</a>.
<hr>
<h2>See Also:</h2>
See <a href="../CONCEPT/storage_class.html">Storage classes</a> to see the more powerfull 
features of variable declarations.

<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>