source: public/doc/gnu-c/Scope.html@ 02598c2

Last change on this file since 02598c2 was 02598c2, checked in by Mikhail Kirillov <w96k@…>, on Oct 6, 2022 at 12:36:29 PM

Add gnu-c

  • Property mode set to 100644
File size: 6.9 KB
Line 
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
6licensed to the FSF.)
7
8Permission is granted to copy, distribute and/or modify this document
9under the terms of the GNU Free Documentation License, Version 1.3 or
10any later version published by the Free Software Foundation; with the
11Invariant Sections being "GNU General Public License," with the
12Front-Cover Texts being "A GNU Manual," and with the Back-Cover
13Texts as in (a) below. A copy of the license is included in the
14section entitled "GNU Free Documentation License."
15
16(a) The FSF's Back-Cover Text is: "You have the freedom to copy and
17modify this GNU manual. Buying copies from the FSF supports it in
18developing 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<!--
37a.summary-letter {text-decoration: none}
38blockquote.indentedblock {margin-right: 0em}
39div.display {margin-left: 3.2em}
40div.example {margin-left: 3.2em}
41div.lisp {margin-left: 3.2em}
42kbd {font-style: oblique}
43pre.display {font-family: inherit}
44pre.format {font-family: inherit}
45pre.menu-comment {font-family: serif}
46pre.menu-preformatted {font-family: serif}
47span.nolinebreak {white-space: nowrap}
48span.roman {font-family: initial; font-weight: normal}
49span.sansserif {font-family: sans-serif; font-weight: normal}
50ul.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>
60Next: <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> &nbsp; [<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
70in certain parts of the program, which is typically less than the whole
71of 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 &ndash; that is,
74not within any blocks and function definitions &ndash; are visible for the
75entire 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
79definitions, 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
84foo (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
91only within the <code>foo</code> function definition block. Thus, other
92blocks could have their own variables, also named <code>x</code>, without
93any conflict between those variables.
94</p>
95<p>A variable declared inside a subblock has a scope limited to
96that subblock,
97</p>
98<div class="example">
99<pre class="example">void
100foo (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
110declared outside of that block, the definition within the block
111takes precedence during its scope:
112</p>
113<div class="example">
114<pre class="example">int x = 42;
115
116void
117foo (void)
118{
119 int x = 17;
120 printf (&quot;%d\n&quot;, x);
121}
122</pre></div>
123
124<p>This prints 17, the value of the variable <code>x</code> declared in the
125function body block, rather than the value of the variable <code>x</code> at
126file scope. We say that the inner declaration of <code>x</code>
127<em>shadows</em> the outer declaration, for the extent of the inner
128declaration&rsquo;s scope.
129</p>
130<p>A declaration with block scope can be shadowed by another declaration
131with the same name in a subblock.
132</p>
133<div class="example">
134<pre class="example">void
135foo (void)
136{
137 char *x = &quot;foo&quot;;
138 {
139 int x = 42;
140 <span class="roman">&hellip;</span>
141 exit (x / 6);
142 }
143}
144</pre></div>
145
146<p>A function parameter&rsquo;s scope is the entire function body, but it can
147be shadowed. For example:
148</p>
149<div class="example">
150<pre class="example">int x = 42;
151
152void
153foo (int x)
154{
155 printf (&quot;%d\n&quot;, x);
156}
157</pre></div>
158
159<p>This prints the value of <code>x</code> the function parameter, rather than
160the 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
163is visible for the whole of the containing function body, both before
164and after the label declaration:
165</p>
166<div class="example">
167<pre class="example">void
168foo (void)
169{
170 <span class="roman">&hellip;</span>
171 goto bar;
172 <span class="roman">&hellip;</span>
173 { // <span class="roman">Subblock does not affect labels.</span>
174 bar:
175 <span class="roman">&hellip;</span>
176 }
177 goto bar;
178}
179</pre></div>
180
181<p>Except for labels, a declared identifier is not
182visible to code before its declaration. For example:
183</p>
184<div class="example">
185<pre class="example">int x = 5;
186int 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;
193int 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>
203Next: <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> &nbsp; [<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>
Note: See TracBrowser for help on using the repository browser.