Include-Operation.html 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  2. <html>
  3. <!-- Copyright (C) 1987-2017 Free Software Foundation, Inc.
  4. Permission is granted to copy, distribute and/or modify this document
  5. under the terms of the GNU Free Documentation License, Version 1.3 or
  6. any later version published by the Free Software Foundation. A copy of
  7. the license is included in the
  8. section entitled "GNU Free Documentation License".
  9. This manual contains no Invariant Sections. The Front-Cover Texts are
  10. (a) (see below), and the Back-Cover Texts are (b) (see below).
  11. (a) The FSF's Front-Cover Text is:
  12. A GNU Manual
  13. (b) The FSF's Back-Cover Text is:
  14. You have freedom to copy and modify this GNU Manual, like GNU
  15. software. Copies published by the Free Software Foundation raise
  16. funds for GNU development. -->
  17. <!-- Created by GNU Texinfo 5.2, http://www.gnu.org/software/texinfo/ -->
  18. <head>
  19. <title>The C Preprocessor: Include Operation</title>
  20. <meta name="description" content="The C Preprocessor: Include Operation">
  21. <meta name="keywords" content="The C Preprocessor: Include Operation">
  22. <meta name="resource-type" content="document">
  23. <meta name="distribution" content="global">
  24. <meta name="Generator" content="makeinfo">
  25. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  26. <link href="index.html#Top" rel="start" title="Top">
  27. <link href="Index-of-Directives.html#Index-of-Directives" rel="index" title="Index of Directives">
  28. <link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
  29. <link href="Header-Files.html#Header-Files" rel="up" title="Header Files">
  30. <link href="Search-Path.html#Search-Path" rel="next" title="Search Path">
  31. <link href="Include-Syntax.html#Include-Syntax" rel="prev" title="Include Syntax">
  32. <style type="text/css">
  33. <!--
  34. a.summary-letter {text-decoration: none}
  35. blockquote.smallquotation {font-size: smaller}
  36. div.display {margin-left: 3.2em}
  37. div.example {margin-left: 3.2em}
  38. div.indentedblock {margin-left: 3.2em}
  39. div.lisp {margin-left: 3.2em}
  40. div.smalldisplay {margin-left: 3.2em}
  41. div.smallexample {margin-left: 3.2em}
  42. div.smallindentedblock {margin-left: 3.2em; font-size: smaller}
  43. div.smalllisp {margin-left: 3.2em}
  44. kbd {font-style:oblique}
  45. pre.display {font-family: inherit}
  46. pre.format {font-family: inherit}
  47. pre.menu-comment {font-family: serif}
  48. pre.menu-preformatted {font-family: serif}
  49. pre.smalldisplay {font-family: inherit; font-size: smaller}
  50. pre.smallexample {font-size: smaller}
  51. pre.smallformat {font-family: inherit; font-size: smaller}
  52. pre.smalllisp {font-size: smaller}
  53. span.nocodebreak {white-space:nowrap}
  54. span.nolinebreak {white-space:nowrap}
  55. span.roman {font-family:serif; font-weight:normal}
  56. span.sansserif {font-family:sans-serif; font-weight:normal}
  57. ul.no-bullet {list-style: none}
  58. -->
  59. </style>
  60. </head>
  61. <body lang="en" bgcolor="#FFFFFF" text="#000000" link="#0000FF" vlink="#800080" alink="#FF0000">
  62. <a name="Include-Operation"></a>
  63. <div class="header">
  64. <p>
  65. Next: <a href="Search-Path.html#Search-Path" accesskey="n" rel="next">Search Path</a>, Previous: <a href="Include-Syntax.html#Include-Syntax" accesskey="p" rel="prev">Include Syntax</a>, Up: <a href="Header-Files.html#Header-Files" accesskey="u" rel="up">Header Files</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Index-of-Directives.html#Index-of-Directives" title="Index" rel="index">Index</a>]</p>
  66. </div>
  67. <hr>
  68. <a name="Include-Operation-1"></a>
  69. <h3 class="section">2.2 Include Operation</h3>
  70. <p>The &lsquo;<samp>#include</samp>&rsquo; directive works by directing the C preprocessor to
  71. scan the specified file as input before continuing with the rest of the
  72. current file. The output from the preprocessor contains the output
  73. already generated, followed by the output resulting from the included
  74. file, followed by the output that comes from the text after the
  75. &lsquo;<samp>#include</samp>&rsquo; directive. For example, if you have a header file
  76. <samp>header.h</samp> as follows,
  77. </p>
  78. <div class="smallexample">
  79. <pre class="smallexample">char *test (void);
  80. </pre></div>
  81. <p>and a main program called <samp>program.c</samp> that uses the header file,
  82. like this,
  83. </p>
  84. <div class="smallexample">
  85. <pre class="smallexample">int x;
  86. #include &quot;header.h&quot;
  87. int
  88. main (void)
  89. {
  90. puts (test ());
  91. }
  92. </pre></div>
  93. <p>the compiler will see the same token stream as it would if
  94. <samp>program.c</samp> read
  95. </p>
  96. <div class="smallexample">
  97. <pre class="smallexample">int x;
  98. char *test (void);
  99. int
  100. main (void)
  101. {
  102. puts (test ());
  103. }
  104. </pre></div>
  105. <p>Included files are not limited to declarations and macro definitions;
  106. those are merely the typical uses. Any fragment of a C program can be
  107. included from another file. The include file could even contain the
  108. beginning of a statement that is concluded in the containing file, or
  109. the end of a statement that was started in the including file. However,
  110. an included file must consist of complete tokens. Comments and string
  111. literals which have not been closed by the end of an included file are
  112. invalid. For error recovery, they are considered to end at the end of
  113. the file.
  114. </p>
  115. <p>To avoid confusion, it is best if header files contain only complete
  116. syntactic units&mdash;function declarations or definitions, type
  117. declarations, etc.
  118. </p>
  119. <p>The line following the &lsquo;<samp>#include</samp>&rsquo; directive is always treated as a
  120. separate line by the C preprocessor, even if the included file lacks a
  121. final newline.
  122. </p>
  123. <hr>
  124. <div class="header">
  125. <p>
  126. Next: <a href="Search-Path.html#Search-Path" accesskey="n" rel="next">Search Path</a>, Previous: <a href="Include-Syntax.html#Include-Syntax" accesskey="p" rel="prev">Include Syntax</a>, Up: <a href="Header-Files.html#Header-Files" accesskey="u" rel="up">Header Files</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Index-of-Directives.html#Index-of-Directives" title="Index" rel="index">Index</a>]</p>
  127. </div>
  128. </body>
  129. </html>