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>Variables (GNU C Language Manual)</title>
|
---|
23 |
|
---|
24 | <meta name="description" content="Variables (GNU C Language Manual)">
|
---|
25 | <meta name="keywords" content="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="index.html" rel="up" title="Top">
|
---|
33 | <link href="Variable-Declarations.html" rel="next" title="Variable Declarations">
|
---|
34 | <link href="Statement-Exprs.html" rel="prev" title="Statement Exprs">
|
---|
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="Variables"></span><div class="header">
|
---|
59 | <p>
|
---|
60 | Next: <a href="Type-Qualifiers.html" accesskey="n" rel="next">Type Qualifiers</a>, Previous: <a href="Statements.html" accesskey="p" rel="prev">Statements</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="Variables-1"></span><h2 class="chapter">20 Variables</h2>
|
---|
64 | <span id="index-variables"></span>
|
---|
65 |
|
---|
66 | <p>Every variable used in a C program needs to be made known by a
|
---|
67 | <em>declaration</em>. It can be used only after it has been declared.
|
---|
68 | It is an error to declare a variable name more than once in the same
|
---|
69 | scope; an exception is that <code>extern</code> declarations and tentative
|
---|
70 | definitions can coexist with another declaration of the same
|
---|
71 | variable.
|
---|
72 | </p>
|
---|
73 | <p>Variables can be declared anywhere within a block or file. (Older
|
---|
74 | versions of C required that all variable declarations within a block
|
---|
75 | occur before any statements.)
|
---|
76 | </p>
|
---|
77 | <p>Variables declared within a function or block are <em>local</em> to
|
---|
78 | it. This means that the variable name is visible only until the end
|
---|
79 | of that function or block, and the memory space is allocated only
|
---|
80 | while control is within it.
|
---|
81 | </p>
|
---|
82 | <p>Variables declared at the top level in a file are called <em>file-scope</em>.
|
---|
83 | They are assigned fixed, distinct memory locations, so they retain
|
---|
84 | their values for the whole execution of the program.
|
---|
85 | </p>
|
---|
86 | <table class="menu" border="0" cellspacing="0">
|
---|
87 | <tr><td align="left" valign="top">• <a href="Variable-Declarations.html" accesskey="1">Variable Declarations</a></td><td> </td><td align="left" valign="top">Name a variable and and reserve space for it.
|
---|
88 | </td></tr>
|
---|
89 | <tr><td align="left" valign="top">• <a href="Initializers.html" accesskey="2">Initializers</a></td><td> </td><td align="left" valign="top">Assigning inital values to variables.
|
---|
90 | </td></tr>
|
---|
91 | <tr><td align="left" valign="top">• <a href="Designated-Inits.html" accesskey="3">Designated Inits</a></td><td> </td><td align="left" valign="top">Assigning initial values to array elements
|
---|
92 | at particular array indices.
|
---|
93 | </td></tr>
|
---|
94 | <tr><td align="left" valign="top">• <a href="Auto-Type.html" accesskey="4">Auto Type</a></td><td> </td><td align="left" valign="top">Obtaining the type of a variable.
|
---|
95 | </td></tr>
|
---|
96 | <tr><td align="left" valign="top">• <a href="Local-Variables.html" accesskey="5">Local Variables</a></td><td> </td><td align="left" valign="top">Variables declared in function definitions.
|
---|
97 | </td></tr>
|
---|
98 | <tr><td align="left" valign="top">• <a href="File_002dScope-Variables.html" accesskey="6">File-Scope Variables</a></td><td> </td><td align="left" valign="top">Variables declared outside of
|
---|
99 | function definitions.
|
---|
100 | </td></tr>
|
---|
101 | <tr><td align="left" valign="top">• <a href="Static-Local-Variables.html" accesskey="7">Static Local Variables</a></td><td> </td><td align="left" valign="top">Variables declared within functions,
|
---|
102 | but with permanent storage allocation.
|
---|
103 | </td></tr>
|
---|
104 | <tr><td align="left" valign="top">• <a href="Extern-Declarations.html" accesskey="8">Extern Declarations</a></td><td> </td><td align="left" valign="top">Declaring a variable
|
---|
105 | which is allocated somewhere else.
|
---|
106 | </td></tr>
|
---|
107 | <tr><td align="left" valign="top">• <a href="Allocating-File_002dScope.html" accesskey="9">Allocating File-Scope</a></td><td> </td><td align="left" valign="top">When is space allocated
|
---|
108 | for file-scope variables?
|
---|
109 | </td></tr>
|
---|
110 | <tr><td align="left" valign="top">• <a href="auto-and-register.html">auto and register</a></td><td> </td><td align="left" valign="top">Historically used storage directions.
|
---|
111 | </td></tr>
|
---|
112 | <tr><td align="left" valign="top">• <a href="Omitting-Types.html">Omitting Types</a></td><td> </td><td align="left" valign="top">The bad practice of declaring variables
|
---|
113 | with implicit type.
|
---|
114 | </td></tr>
|
---|
115 | </table>
|
---|
116 |
|
---|
117 | <hr>
|
---|
118 | <div class="header">
|
---|
119 | <p>
|
---|
120 | Next: <a href="Type-Qualifiers.html" accesskey="n" rel="next">Type Qualifiers</a>, Previous: <a href="Statements.html" accesskey="p" rel="prev">Statements</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>
|
---|
121 | </div>
|
---|
122 |
|
---|
123 |
|
---|
124 |
|
---|
125 | </body>
|
---|
126 | </html>
|
---|