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>Local Variables (GNU C Language Manual)</title>
|
---|
23 |
|
---|
24 | <meta name="description" content="Local Variables (GNU C Language Manual)">
|
---|
25 | <meta name="keywords" content="Local Variables (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="Variables.html" rel="up" title="Variables">
|
---|
33 | <link href="File_002dScope-Variables.html" rel="next" title="File-Scope Variables">
|
---|
34 | <link href="Auto-Type.html" rel="prev" title="Auto 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="Local-Variables"></span><div class="header">
|
---|
59 | <p>
|
---|
60 | Next: <a href="File_002dScope-Variables.html" accesskey="n" rel="next">File-Scope Variables</a>, Previous: <a href="Auto-Type.html" accesskey="p" rel="prev">Auto Type</a>, Up: <a href="Variables.html" accesskey="u" rel="up">Variables</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="Local-Variables-1"></span><h3 class="section">20.5 Local Variables</h3>
|
---|
64 | <span id="index-local-variables"></span>
|
---|
65 | <span id="index-variables_002c-local"></span>
|
---|
66 |
|
---|
67 | <p>Declaring a variable inside a function definition (see <a href="Function-Definitions.html">Function Definitions</a>) makes the variable name <em>local</em> to the containing
|
---|
68 | block—that is, the containing pair of braces. More precisely, the
|
---|
69 | variable’s name is visible starting just after where it appears in the
|
---|
70 | declaration, and its visibility continues until the end of the block.
|
---|
71 | </p>
|
---|
72 | <p>Local variables in C are generally <em>automatic</em> variables: each
|
---|
73 | variable’s storage exists only from the declaration to the end of the
|
---|
74 | block. Execution of the declaration allocates the storage, computes
|
---|
75 | the initial value, and stores it in the variable. The end of the
|
---|
76 | block deallocates the storage.<a id="DOCF6" href="#FOOT6"><sup>6</sup></a>
|
---|
77 | </p>
|
---|
78 | <p><strong>Warning:</strong> Two declarations for the same local variable
|
---|
79 | in the same scope are an error.
|
---|
80 | </p>
|
---|
81 | <p><strong>Warning:</strong> Automatic variables are stored in the run-time stack.
|
---|
82 | The total space for the program’s stack may be limited; therefore,
|
---|
83 | in using very large arrays, it may be necessary to allocate
|
---|
84 | them in some other way to stop the program from crashing.
|
---|
85 | </p>
|
---|
86 | <p><strong>Warning:</strong> If the declaration of an automatic variable does not
|
---|
87 | specify an initial value, the variable starts out containing garbage.
|
---|
88 | In this example, the value printed could be anything at all:
|
---|
89 | </p>
|
---|
90 | <div class="example">
|
---|
91 | <pre class="example">{
|
---|
92 | int i;
|
---|
93 |
|
---|
94 | printf ("Print junk %d\n", i);
|
---|
95 | }
|
---|
96 | </pre></div>
|
---|
97 |
|
---|
98 | <p>In a simple test program, that statement is likely to print 0, simply
|
---|
99 | because every process starts with memory zeroed. But don’t rely on it
|
---|
100 | to be zero—that is erroneous.
|
---|
101 | </p>
|
---|
102 | <p><strong>Note:</strong> Make sure to store a value into each local variable (by
|
---|
103 | assignment, or by initialization) before referring to its value.
|
---|
104 | </p>
|
---|
105 | <div class="footnote">
|
---|
106 | <hr>
|
---|
107 | <h4 class="footnotes-heading">Footnotes</h4>
|
---|
108 |
|
---|
109 | <h5><a id="FOOT6" href="#DOCF6">(6)</a></h3>
|
---|
110 | <p>Due to compiler optimizations,
|
---|
111 | allocation and deallocation don’t necessarily really happen at
|
---|
112 | those times.</p>
|
---|
113 | </div>
|
---|
114 | <hr>
|
---|
115 | <div class="header">
|
---|
116 | <p>
|
---|
117 | Next: <a href="File_002dScope-Variables.html" accesskey="n" rel="next">File-Scope Variables</a>, Previous: <a href="Auto-Type.html" accesskey="p" rel="prev">Auto Type</a>, Up: <a href="Variables.html" accesskey="u" rel="up">Variables</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>
|
---|
118 | </div>
|
---|
119 |
|
---|
120 |
|
---|
121 |
|
---|
122 | </body>
|
---|
123 | </html>
|
---|