Initial-processing.html 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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: Initial processing</title>
  20. <meta name="description" content="The C Preprocessor: Initial processing">
  21. <meta name="keywords" content="The C Preprocessor: Initial processing">
  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="Overview.html#Overview" rel="up" title="Overview">
  30. <link href="Tokenization.html#Tokenization" rel="next" title="Tokenization">
  31. <link href="Character-sets.html#Character-sets" rel="prev" title="Character sets">
  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="Initial-processing"></a>
  63. <div class="header">
  64. <p>
  65. Next: <a href="Tokenization.html#Tokenization" accesskey="n" rel="next">Tokenization</a>, Previous: <a href="Character-sets.html#Character-sets" accesskey="p" rel="prev">Character sets</a>, Up: <a href="Overview.html#Overview" accesskey="u" rel="up">Overview</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="Initial-processing-1"></a>
  69. <h3 class="section">1.2 Initial processing</h3>
  70. <p>The preprocessor performs a series of textual transformations on its
  71. input. These happen before all other processing. Conceptually, they
  72. happen in a rigid order, and the entire file is run through each
  73. transformation before the next one begins. CPP actually does them
  74. all at once, for performance reasons. These transformations correspond
  75. roughly to the first three &ldquo;phases of translation&rdquo; described in the C
  76. standard.
  77. </p>
  78. <ol>
  79. <li> <a name="index-line-endings"></a>
  80. The input file is read into memory and broken into lines.
  81. <p>Different systems use different conventions to indicate the end of a
  82. line. GCC accepts the ASCII control sequences <kbd>LF</kbd>, <kbd>CR&nbsp;LF<!-- /@w --></kbd> and <kbd>CR</kbd> as end-of-line markers. These are the canonical
  83. sequences used by Unix, DOS and VMS, and the classic Mac OS (before
  84. OSX) respectively. You may therefore safely copy source code written
  85. on any of those systems to a different one and use it without
  86. conversion. (GCC may lose track of the current line number if a file
  87. doesn&rsquo;t consistently use one convention, as sometimes happens when it
  88. is edited on computers with different conventions that share a network
  89. file system.)
  90. </p>
  91. <p>If the last line of any input file lacks an end-of-line marker, the end
  92. of the file is considered to implicitly supply one. The C standard says
  93. that this condition provokes undefined behavior, so GCC will emit a
  94. warning message.
  95. </p>
  96. </li><li> <a name="index-trigraphs"></a>
  97. <a name="trigraphs"></a>If trigraphs are enabled, they are replaced by their
  98. corresponding single characters. By default GCC ignores trigraphs,
  99. but if you request a strictly conforming mode with the <samp>-std</samp>
  100. option, or you specify the <samp>-trigraphs</samp> option, then it
  101. converts them.
  102. <p>These are nine three-character sequences, all starting with &lsquo;<samp>??</samp>&rsquo;,
  103. that are defined by ISO C to stand for single characters. They permit
  104. obsolete systems that lack some of C&rsquo;s punctuation to use C. For
  105. example, &lsquo;<samp>??/</samp>&rsquo; stands for &lsquo;<samp>\</samp>&rsquo;, so <tt>'??/n'</tt> is a character
  106. constant for a newline.
  107. </p>
  108. <p>Trigraphs are not popular and many compilers implement them
  109. incorrectly. Portable code should not rely on trigraphs being either
  110. converted or ignored. With <samp>-Wtrigraphs</samp> GCC will warn you
  111. when a trigraph may change the meaning of your program if it were
  112. converted. See <a href="Invocation.html#Wtrigraphs">Wtrigraphs</a>.
  113. </p>
  114. <p>In a string constant, you can prevent a sequence of question marks
  115. from being confused with a trigraph by inserting a backslash between
  116. the question marks, or by separating the string literal at the
  117. trigraph and making use of string literal concatenation. <tt>&quot;(??\?)&quot;</tt>
  118. is the string &lsquo;<samp>(???)</samp>&rsquo;, not &lsquo;<samp>(?]</samp>&rsquo;. Traditional C compilers
  119. do not recognize these idioms.
  120. </p>
  121. <p>The nine trigraphs and their replacements are
  122. </p>
  123. <div class="smallexample">
  124. <pre class="smallexample">Trigraph: ??( ??) ??&lt; ??&gt; ??= ??/ ??' ??! ??-
  125. Replacement: [ ] { } # \ ^ | ~
  126. </pre></div>
  127. </li><li> <a name="index-continued-lines"></a>
  128. <a name="index-backslash_002dnewline"></a>
  129. Continued lines are merged into one long line.
  130. <p>A continued line is a line which ends with a backslash, &lsquo;<samp>\</samp>&rsquo;. The
  131. backslash is removed and the following line is joined with the current
  132. one. No space is inserted, so you may split a line anywhere, even in
  133. the middle of a word. (It is generally more readable to split lines
  134. only at white space.)
  135. </p>
  136. <p>The trailing backslash on a continued line is commonly referred to as a
  137. <em>backslash-newline</em>.
  138. </p>
  139. <p>If there is white space between a backslash and the end of a line, that
  140. is still a continued line. However, as this is usually the result of an
  141. editing mistake, and many compilers will not accept it as a continued
  142. line, GCC will warn you about it.
  143. </p>
  144. </li><li> <a name="index-comments"></a>
  145. <a name="index-line-comments"></a>
  146. <a name="index-block-comments"></a>
  147. All comments are replaced with single spaces.
  148. <p>There are two kinds of comments. <em>Block comments</em> begin with
  149. &lsquo;<samp>/*</samp>&rsquo; and continue until the next &lsquo;<samp>*/</samp>&rsquo;. Block comments do not
  150. nest:
  151. </p>
  152. <div class="smallexample">
  153. <pre class="smallexample">/* <span class="roman">this is</span> /* <span class="roman">one comment</span> */ <span class="roman">text outside comment</span>
  154. </pre></div>
  155. <p><em>Line comments</em> begin with &lsquo;<samp>//</samp>&rsquo; and continue to the end of the
  156. current line. Line comments do not nest either, but it does not matter,
  157. because they would end in the same place anyway.
  158. </p>
  159. <div class="smallexample">
  160. <pre class="smallexample">// <span class="roman">this is</span> // <span class="roman">one comment</span>
  161. <span class="roman">text outside comment</span>
  162. </pre></div>
  163. </li></ol>
  164. <p>It is safe to put line comments inside block comments, or vice versa.
  165. </p>
  166. <div class="smallexample">
  167. <pre class="smallexample">/* <span class="roman">block comment</span>
  168. // <span class="roman">contains line comment</span>
  169. <span class="roman">yet more comment</span>
  170. */ <span class="roman">outside comment</span>
  171. // <span class="roman">line comment</span> /* <span class="roman">contains block comment</span> */
  172. </pre></div>
  173. <p>But beware of commenting out one end of a block comment with a line
  174. comment.
  175. </p>
  176. <div class="smallexample">
  177. <pre class="smallexample"> // <span class="roman">l.c.</span> /* <span class="roman">block comment begins</span>
  178. <span class="roman">oops! this isn&rsquo;t a comment anymore</span> */
  179. </pre></div>
  180. <p>Comments are not recognized within string literals.
  181. <tt>&quot;/*&nbsp;blah&nbsp;*/&quot;<!-- /@w --></tt> is the string constant &lsquo;<samp>/*&nbsp;blah&nbsp;*/<!-- /@w --></samp>&rsquo;, not
  182. an empty string.
  183. </p>
  184. <p>Line comments are not in the 1989 edition of the C standard, but they
  185. are recognized by GCC as an extension. In C++ and in the 1999 edition
  186. of the C standard, they are an official part of the language.
  187. </p>
  188. <p>Since these transformations happen before all other processing, you can
  189. split a line mechanically with backslash-newline anywhere. You can
  190. comment out the end of a line. You can continue a line comment onto the
  191. next line with backslash-newline. You can even split &lsquo;<samp>/*</samp>&rsquo;,
  192. &lsquo;<samp>*/</samp>&rsquo;, and &lsquo;<samp>//</samp>&rsquo; onto multiple lines with backslash-newline.
  193. For example:
  194. </p>
  195. <div class="smallexample">
  196. <pre class="smallexample">/\
  197. *
  198. */ # /*
  199. */ defi\
  200. ne FO\
  201. O 10\
  202. 20
  203. </pre></div>
  204. <p>is equivalent to <code>#define&nbsp;FOO&nbsp;1020<!-- /@w --></code>. All these tricks are
  205. extremely confusing and should not be used in code intended to be
  206. readable.
  207. </p>
  208. <p>There is no way to prevent a backslash at the end of a line from being
  209. interpreted as a backslash-newline. This cannot affect any correct
  210. program, however.
  211. </p>
  212. <hr>
  213. <div class="header">
  214. <p>
  215. Next: <a href="Tokenization.html#Tokenization" accesskey="n" rel="next">Tokenization</a>, Previous: <a href="Character-sets.html#Character-sets" accesskey="p" rel="prev">Character sets</a>, Up: <a href="Overview.html#Overview" accesskey="u" rel="up">Overview</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>
  216. </div>
  217. </body>
  218. </html>