The-preprocessing-language.html 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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: The preprocessing language</title>
  20. <meta name="description" content="The C Preprocessor: The preprocessing language">
  21. <meta name="keywords" content="The C Preprocessor: The preprocessing language">
  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="Header-Files.html#Header-Files" rel="next" title="Header Files">
  31. <link href="Tokenization.html#Tokenization" rel="prev" title="Tokenization">
  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="The-preprocessing-language"></a>
  63. <div class="header">
  64. <p>
  65. Previous: <a href="Tokenization.html#Tokenization" accesskey="p" rel="prev">Tokenization</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="The-preprocessing-language-1"></a>
  69. <h3 class="section">1.4 The preprocessing language</h3>
  70. <a name="index-directives"></a>
  71. <a name="index-preprocessing-directives"></a>
  72. <a name="index-directive-line"></a>
  73. <a name="index-directive-name"></a>
  74. <p>After tokenization, the stream of tokens may simply be passed straight
  75. to the compiler&rsquo;s parser. However, if it contains any operations in the
  76. <em>preprocessing language</em>, it will be transformed first. This stage
  77. corresponds roughly to the standard&rsquo;s &ldquo;translation phase 4&rdquo; and is
  78. what most people think of as the preprocessor&rsquo;s job.
  79. </p>
  80. <p>The preprocessing language consists of <em>directives</em> to be executed
  81. and <em>macros</em> to be expanded. Its primary capabilities are:
  82. </p>
  83. <ul>
  84. <li> Inclusion of header files. These are files of declarations that can be
  85. substituted into your program.
  86. </li><li> Macro expansion. You can define <em>macros</em>, which are abbreviations
  87. for arbitrary fragments of C code. The preprocessor will replace the
  88. macros with their definitions throughout the program. Some macros are
  89. automatically defined for you.
  90. </li><li> Conditional compilation. You can include or exclude parts of the
  91. program according to various conditions.
  92. </li><li> Line control. If you use a program to combine or rearrange source files
  93. into an intermediate file which is then compiled, you can use line
  94. control to inform the compiler where each source line originally came
  95. from.
  96. </li><li> Diagnostics. You can detect problems at compile time and issue errors
  97. or warnings.
  98. </li></ul>
  99. <p>There are a few more, less useful, features.
  100. </p>
  101. <p>Except for expansion of predefined macros, all these operations are
  102. triggered with <em>preprocessing directives</em>. Preprocessing directives
  103. are lines in your program that start with &lsquo;<samp>#</samp>&rsquo;. Whitespace is
  104. allowed before and after the &lsquo;<samp>#</samp>&rsquo;. The &lsquo;<samp>#</samp>&rsquo; is followed by an
  105. identifier, the <em>directive name</em>. It specifies the operation to
  106. perform. Directives are commonly referred to as &lsquo;<samp>#<var>name</var></samp>&rsquo;
  107. where <var>name</var> is the directive name. For example, &lsquo;<samp>#define</samp>&rsquo; is
  108. the directive that defines a macro.
  109. </p>
  110. <p>The &lsquo;<samp>#</samp>&rsquo; which begins a directive cannot come from a macro
  111. expansion. Also, the directive name is not macro expanded. Thus, if
  112. <code>foo</code> is defined as a macro expanding to <code>define</code>, that does
  113. not make &lsquo;<samp>#foo</samp>&rsquo; a valid preprocessing directive.
  114. </p>
  115. <p>The set of valid directive names is fixed. Programs cannot define new
  116. preprocessing directives.
  117. </p>
  118. <p>Some directives require arguments; these make up the rest of the
  119. directive line and must be separated from the directive name by
  120. whitespace. For example, &lsquo;<samp>#define</samp>&rsquo; must be followed by a macro
  121. name and the intended expansion of the macro.
  122. </p>
  123. <p>A preprocessing directive cannot cover more than one line. The line
  124. may, however, be continued with backslash-newline, or by a block comment
  125. which extends past the end of the line. In either case, when the
  126. directive is processed, the continuations have already been merged with
  127. the first line to make one long line.
  128. </p>
  129. <hr>
  130. <div class="header">
  131. <p>
  132. Previous: <a href="Tokenization.html#Tokenization" accesskey="p" rel="prev">Tokenization</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>
  133. </div>
  134. </body>
  135. </html>