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>include Operation (GNU C Language Manual)</title>
|
---|
23 |
|
---|
24 | <meta name="description" content="include Operation (GNU C Language Manual)">
|
---|
25 | <meta name="keywords" content="include Operation (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="Header-Files.html" rel="up" title="Header Files">
|
---|
33 | <link href="Search-Path.html" rel="next" title="Search Path">
|
---|
34 | <link href="include-Syntax.html" rel="prev" title="include Syntax">
|
---|
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="include-Operation"></span><div class="header">
|
---|
59 | <p>
|
---|
60 | Next: <a href="Search-Path.html" accesskey="n" rel="next">Search Path</a>, Previous: <a href="include-Syntax.html" accesskey="p" rel="prev">include Syntax</a>, Up: <a href="Header-Files.html" accesskey="u" rel="up">Header Files</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="g_t_0023include-Operation"></span><h4 class="subsection">26.4.2 <code>#include</code> Operation</h4>
|
---|
64 |
|
---|
65 | <p>The <code>#include</code> directive works by scanning the specified header
|
---|
66 | file as input before continuing with the rest of the current file.
|
---|
67 | The result of preprocessing consists of the text already generated,
|
---|
68 | followed by the result of preprocessing the included file, followed by
|
---|
69 | whatever results from the text after the <code>#include</code> directive.
|
---|
70 | For example, if you have a header file <samp>header.h</samp> as follows,
|
---|
71 | </p>
|
---|
72 | <div class="example">
|
---|
73 | <pre class="example">char *test (void);
|
---|
74 | </pre></div>
|
---|
75 |
|
---|
76 | <p>and a main program called <samp>program.c</samp> that uses the header file,
|
---|
77 | like this,
|
---|
78 | </p>
|
---|
79 | <div class="example">
|
---|
80 | <pre class="example">int x;
|
---|
81 | #include "header.h"
|
---|
82 |
|
---|
83 | int
|
---|
84 | main (void)
|
---|
85 | {
|
---|
86 | puts (test ());
|
---|
87 | }
|
---|
88 | </pre></div>
|
---|
89 |
|
---|
90 | <p>the result is equivalent to putting this text in <samp>program.c</samp>:
|
---|
91 | </p>
|
---|
92 | <div class="example">
|
---|
93 | <pre class="example">int x;
|
---|
94 | char *test (void);
|
---|
95 |
|
---|
96 | int
|
---|
97 | main (void)
|
---|
98 | {
|
---|
99 | puts (test ());
|
---|
100 | }
|
---|
101 | </pre></div>
|
---|
102 |
|
---|
103 | <p>Included files are not limited to declarations and macro definitions;
|
---|
104 | those are merely the typical uses. Any fragment of a C program can be
|
---|
105 | included from another file. The include file could even contain the
|
---|
106 | beginning of a statement that is concluded in the containing file, or
|
---|
107 | the end of a statement that was started in the including file. However,
|
---|
108 | an included file must consist of complete tokens. Comments and string
|
---|
109 | literals that have not been closed by the end of an included file are
|
---|
110 | invalid. For error recovery, the compiler terminates them at the end of
|
---|
111 | the file.
|
---|
112 | </p>
|
---|
113 | <p>To avoid confusion, it is best if header files contain only complete
|
---|
114 | syntactic units—function declarations or definitions, type
|
---|
115 | declarations, etc.
|
---|
116 | </p>
|
---|
117 | <p>The line following the <code>#include</code> directive is always treated as
|
---|
118 | a separate line, even if the included file lacks a final newline.
|
---|
119 | There is no problem putting a preprocessing directive there.
|
---|
120 | </p>
|
---|
121 | <hr>
|
---|
122 | <div class="header">
|
---|
123 | <p>
|
---|
124 | Next: <a href="Search-Path.html" accesskey="n" rel="next">Search Path</a>, Previous: <a href="include-Syntax.html" accesskey="p" rel="prev">include Syntax</a>, Up: <a href="Header-Files.html" accesskey="u" rel="up">Header Files</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>
|
---|
125 | </div>
|
---|
126 |
|
---|
127 |
|
---|
128 |
|
---|
129 | </body>
|
---|
130 | </html>
|
---|