1 | <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
|
---|
2 | <html>
|
---|
3 | <!-- Copyright (C) 2022 Richard Stallman and Free Software Foundation, Inc.
|
---|
4 |
|
---|
5 | (The work of Trevis Rothwell and Nelson Beebe has been assigned or
|
---|
6 | licensed to the FSF.)
|
---|
7 |
|
---|
8 | Permission is granted to copy, distribute and/or modify this document
|
---|
9 | under the terms of the GNU Free Documentation License, Version 1.3 or
|
---|
10 | any later version published by the Free Software Foundation; with the
|
---|
11 | Invariant Sections being "GNU General Public License," with the
|
---|
12 | Front-Cover Texts being "A GNU Manual," and with the Back-Cover
|
---|
13 | Texts as in (a) below. A copy of the license is included in the
|
---|
14 | section entitled "GNU Free Documentation License."
|
---|
15 |
|
---|
16 | (a) The FSF's Back-Cover Text is: "You have the freedom to copy and
|
---|
17 | modify this GNU manual. Buying copies from the FSF supports it in
|
---|
18 | developing GNU and promoting software freedom." -->
|
---|
19 | <!-- Created by GNU Texinfo 6.7, http://www.gnu.org/software/texinfo/ -->
|
---|
20 | <head>
|
---|
21 | <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
|
---|
22 | <title>Scope (GNU C Language Manual)</title>
|
---|
23 |
|
---|
24 | <meta name="description" content="Scope (GNU C Language Manual)">
|
---|
25 | <meta name="keywords" content="Scope (GNU C Language Manual)">
|
---|
26 | <meta name="resource-type" content="document">
|
---|
27 | <meta name="distribution" content="global">
|
---|
28 | <meta name="Generator" content="makeinfo">
|
---|
29 | <link href="index.html" rel="start" title="Top">
|
---|
30 | <link href="Symbol-Index.html" rel="index" title="Symbol Index">
|
---|
31 | <link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
|
---|
32 | <link href="index.html" rel="up" title="Top">
|
---|
33 | <link href="Preprocessing.html" rel="next" title="Preprocessing">
|
---|
34 | <link href="Common-Type.html" rel="prev" title="Common Type">
|
---|
35 | <style type="text/css">
|
---|
36 | <!--
|
---|
37 | a.summary-letter {text-decoration: none}
|
---|
38 | blockquote.indentedblock {margin-right: 0em}
|
---|
39 | div.display {margin-left: 3.2em}
|
---|
40 | div.example {margin-left: 3.2em}
|
---|
41 | div.lisp {margin-left: 3.2em}
|
---|
42 | kbd {font-style: oblique}
|
---|
43 | pre.display {font-family: inherit}
|
---|
44 | pre.format {font-family: inherit}
|
---|
45 | pre.menu-comment {font-family: serif}
|
---|
46 | pre.menu-preformatted {font-family: serif}
|
---|
47 | span.nolinebreak {white-space: nowrap}
|
---|
48 | span.roman {font-family: initial; font-weight: normal}
|
---|
49 | span.sansserif {font-family: sans-serif; font-weight: normal}
|
---|
50 | ul.no-bullet {list-style: none}
|
---|
51 | -->
|
---|
52 | </style>
|
---|
53 |
|
---|
54 |
|
---|
55 | </head>
|
---|
56 |
|
---|
57 | <body lang="en">
|
---|
58 | <span id="Scope"></span><div class="header">
|
---|
59 | <p>
|
---|
60 | Next: <a href="Preprocessing.html" accesskey="n" rel="next">Preprocessing</a>, Previous: <a href="Type-Conversions.html" accesskey="p" rel="prev">Type Conversions</a>, Up: <a href="index.html" accesskey="u" rel="up">Top</a> [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Symbol-Index.html" title="Index" rel="index">Index</a>]</p>
|
---|
61 | </div>
|
---|
62 | <hr>
|
---|
63 | <span id="Scope-1"></span><h2 class="chapter">25 Scope</h2>
|
---|
64 | <span id="index-scope"></span>
|
---|
65 | <span id="index-block-scope"></span>
|
---|
66 | <span id="index-function-scope"></span>
|
---|
67 | <span id="index-function-prototype-scope"></span>
|
---|
68 |
|
---|
69 | <p>Each definition or declaration of an identifier is visible
|
---|
70 | in certain parts of the program, which is typically less than the whole
|
---|
71 | of the program. The parts where it is visible are called its <em>scope</em>.
|
---|
72 | </p>
|
---|
73 | <p>Normally, declarations made at the top-level in the source – that is,
|
---|
74 | not within any blocks and function definitions – are visible for the
|
---|
75 | entire contents of the source file after that point. This is called
|
---|
76 | <em>file scope</em> (see <a href="File_002dScope-Variables.html">File-Scope Variables</a>).
|
---|
77 | </p>
|
---|
78 | <p>Declarations made within blocks of code, including within function
|
---|
79 | definitions, are visible only within those blocks. This is called
|
---|
80 | <em>block scope</em>. Here is an example:
|
---|
81 | </p>
|
---|
82 | <div class="example">
|
---|
83 | <pre class="example">void
|
---|
84 | foo (void)
|
---|
85 | {
|
---|
86 | int x = 42;
|
---|
87 | }
|
---|
88 | </pre></div>
|
---|
89 |
|
---|
90 | <p>In this example, the variable <code>x</code> has block scope; it is visible
|
---|
91 | only within the <code>foo</code> function definition block. Thus, other
|
---|
92 | blocks could have their own variables, also named <code>x</code>, without
|
---|
93 | any conflict between those variables.
|
---|
94 | </p>
|
---|
95 | <p>A variable declared inside a subblock has a scope limited to
|
---|
96 | that subblock,
|
---|
97 | </p>
|
---|
98 | <div class="example">
|
---|
99 | <pre class="example">void
|
---|
100 | foo (void)
|
---|
101 | {
|
---|
102 | {
|
---|
103 | int x = 42;
|
---|
104 | }
|
---|
105 | // <span class="roman"><code>x</code> is out of scope here.</span>
|
---|
106 | }
|
---|
107 | </pre></div>
|
---|
108 |
|
---|
109 | <p>If a variable declared within a block has the same name as a variable
|
---|
110 | declared outside of that block, the definition within the block
|
---|
111 | takes precedence during its scope:
|
---|
112 | </p>
|
---|
113 | <div class="example">
|
---|
114 | <pre class="example">int x = 42;
|
---|
115 |
|
---|
116 | void
|
---|
117 | foo (void)
|
---|
118 | {
|
---|
119 | int x = 17;
|
---|
120 | printf ("%d\n", x);
|
---|
121 | }
|
---|
122 | </pre></div>
|
---|
123 |
|
---|
124 | <p>This prints 17, the value of the variable <code>x</code> declared in the
|
---|
125 | function body block, rather than the value of the variable <code>x</code> at
|
---|
126 | file scope. We say that the inner declaration of <code>x</code>
|
---|
127 | <em>shadows</em> the outer declaration, for the extent of the inner
|
---|
128 | declaration’s scope.
|
---|
129 | </p>
|
---|
130 | <p>A declaration with block scope can be shadowed by another declaration
|
---|
131 | with the same name in a subblock.
|
---|
132 | </p>
|
---|
133 | <div class="example">
|
---|
134 | <pre class="example">void
|
---|
135 | foo (void)
|
---|
136 | {
|
---|
137 | char *x = "foo";
|
---|
138 | {
|
---|
139 | int x = 42;
|
---|
140 | <span class="roman">…</span>
|
---|
141 | exit (x / 6);
|
---|
142 | }
|
---|
143 | }
|
---|
144 | </pre></div>
|
---|
145 |
|
---|
146 | <p>A function parameter’s scope is the entire function body, but it can
|
---|
147 | be shadowed. For example:
|
---|
148 | </p>
|
---|
149 | <div class="example">
|
---|
150 | <pre class="example">int x = 42;
|
---|
151 |
|
---|
152 | void
|
---|
153 | foo (int x)
|
---|
154 | {
|
---|
155 | printf ("%d\n", x);
|
---|
156 | }
|
---|
157 | </pre></div>
|
---|
158 |
|
---|
159 | <p>This prints the value of <code>x</code> the function parameter, rather than
|
---|
160 | the value of the file-scope variable <code>x</code>. However,
|
---|
161 | </p>
|
---|
162 | <p>Labels (see <a href="goto-Statement.html">goto Statement</a>) have <em>function</em> scope: each label
|
---|
163 | is visible for the whole of the containing function body, both before
|
---|
164 | and after the label declaration:
|
---|
165 | </p>
|
---|
166 | <div class="example">
|
---|
167 | <pre class="example">void
|
---|
168 | foo (void)
|
---|
169 | {
|
---|
170 | <span class="roman">…</span>
|
---|
171 | goto bar;
|
---|
172 | <span class="roman">…</span>
|
---|
173 | { // <span class="roman">Subblock does not affect labels.</span>
|
---|
174 | bar:
|
---|
175 | <span class="roman">…</span>
|
---|
176 | }
|
---|
177 | goto bar;
|
---|
178 | }
|
---|
179 | </pre></div>
|
---|
180 |
|
---|
181 | <p>Except for labels, a declared identifier is not
|
---|
182 | visible to code before its declaration. For example:
|
---|
183 | </p>
|
---|
184 | <div class="example">
|
---|
185 | <pre class="example">int x = 5;
|
---|
186 | int y = x + 10;
|
---|
187 | </pre></div>
|
---|
188 |
|
---|
189 | <p>will work, but:
|
---|
190 | </p>
|
---|
191 | <div class="example">
|
---|
192 | <pre class="example">int x = y + 10;
|
---|
193 | int y = 5;
|
---|
194 | </pre></div>
|
---|
195 |
|
---|
196 | <p>cannot refer to the variable <code>y</code> before its declaration.
|
---|
197 | </p>
|
---|
198 |
|
---|
199 |
|
---|
200 | <hr>
|
---|
201 | <div class="header">
|
---|
202 | <p>
|
---|
203 | Next: <a href="Preprocessing.html" accesskey="n" rel="next">Preprocessing</a>, Previous: <a href="Type-Conversions.html" accesskey="p" rel="prev">Type Conversions</a>, Up: <a href="index.html" accesskey="u" rel="up">Top</a> [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Symbol-Index.html" title="Index" rel="index">Index</a>]</p>
|
---|
204 | </div>
|
---|
205 |
|
---|
206 |
|
---|
207 |
|
---|
208 | </body>
|
---|
209 | </html>
|
---|