The-preprocessing-language.html 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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-2023 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 6.7, http://www.gnu.org/software/texinfo/ -->
  18. <head>
  19. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  20. <title>The preprocessing language (The C Preprocessor)</title>
  21. <meta name="description" content="The preprocessing language (The C Preprocessor)">
  22. <meta name="keywords" content="The preprocessing language (The C Preprocessor)">
  23. <meta name="resource-type" content="document">
  24. <meta name="distribution" content="global">
  25. <meta name="Generator" content="makeinfo">
  26. <link href="index.html" rel="start" title="Top">
  27. <link href="Index-of-Directives.html" rel="index" title="Index of Directives">
  28. <link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
  29. <link href="Overview.html" rel="up" title="Overview">
  30. <link href="Header-Files.html" rel="next" title="Header Files">
  31. <link href="Tokenization.html" rel="prev" title="Tokenization">
  32. <style type="text/css">
  33. <!--
  34. a.summary-letter {text-decoration: none}
  35. blockquote.indentedblock {margin-right: 0em}
  36. div.display {margin-left: 3.2em}
  37. div.example {margin-left: 3.2em}
  38. div.lisp {margin-left: 3.2em}
  39. kbd {font-style: oblique}
  40. pre.display {font-family: inherit}
  41. pre.format {font-family: inherit}
  42. pre.menu-comment {font-family: serif}
  43. pre.menu-preformatted {font-family: serif}
  44. span.nolinebreak {white-space: nowrap}
  45. span.roman {font-family: initial; font-weight: normal}
  46. span.sansserif {font-family: sans-serif; font-weight: normal}
  47. ul.no-bullet {list-style: none}
  48. -->
  49. </style>
  50. </head>
  51. <body lang="en">
  52. <span id="The-preprocessing-language"></span><div class="header">
  53. <p>
  54. Previous: <a href="Tokenization.html" accesskey="p" rel="prev">Tokenization</a>, Up: <a href="Overview.html" 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" title="Index" rel="index">Index</a>]</p>
  55. </div>
  56. <hr>
  57. <span id="The-preprocessing-language-1"></span><h3 class="section">1.4 The preprocessing language</h3>
  58. <span id="index-directives"></span>
  59. <span id="index-preprocessing-directives"></span>
  60. <span id="index-directive-line"></span>
  61. <span id="index-directive-name"></span>
  62. <p>After tokenization, the stream of tokens may simply be passed straight
  63. to the compiler&rsquo;s parser. However, if it contains any operations in the
  64. <em>preprocessing language</em>, it will be transformed first. This stage
  65. corresponds roughly to the standard&rsquo;s &ldquo;translation phase 4&rdquo; and is
  66. what most people think of as the preprocessor&rsquo;s job.
  67. </p>
  68. <p>The preprocessing language consists of <em>directives</em> to be executed
  69. and <em>macros</em> to be expanded. Its primary capabilities are:
  70. </p>
  71. <ul>
  72. <li> Inclusion of header files. These are files of declarations that can be
  73. substituted into your program.
  74. </li><li> Macro expansion. You can define <em>macros</em>, which are abbreviations
  75. for arbitrary fragments of C code. The preprocessor will replace the
  76. macros with their definitions throughout the program. Some macros are
  77. automatically defined for you.
  78. </li><li> Conditional compilation. You can include or exclude parts of the
  79. program according to various conditions.
  80. </li><li> Line control. If you use a program to combine or rearrange source files
  81. into an intermediate file which is then compiled, you can use line
  82. control to inform the compiler where each source line originally came
  83. from.
  84. </li><li> Diagnostics. You can detect problems at compile time and issue errors
  85. or warnings.
  86. </li></ul>
  87. <p>There are a few more, less useful, features.
  88. </p>
  89. <p>Except for expansion of predefined macros, all these operations are
  90. triggered with <em>preprocessing directives</em>. Preprocessing directives
  91. are lines in your program that start with &lsquo;<samp>#</samp>&rsquo;. Whitespace is
  92. allowed before and after the &lsquo;<samp>#</samp>&rsquo;. The &lsquo;<samp>#</samp>&rsquo; is followed by an
  93. identifier, the <em>directive name</em>. It specifies the operation to
  94. perform. Directives are commonly referred to as &lsquo;<samp>#<var>name</var></samp>&rsquo;
  95. where <var>name</var> is the directive name. For example, &lsquo;<samp>#define</samp>&rsquo; is
  96. the directive that defines a macro.
  97. </p>
  98. <p>The &lsquo;<samp>#</samp>&rsquo; which begins a directive cannot come from a macro
  99. expansion. Also, the directive name is not macro expanded. Thus, if
  100. <code>foo</code> is defined as a macro expanding to <code>define</code>, that does
  101. not make &lsquo;<samp>#foo</samp>&rsquo; a valid preprocessing directive.
  102. </p>
  103. <p>The set of valid directive names is fixed. Programs cannot define new
  104. preprocessing directives.
  105. </p>
  106. <p>Some directives require arguments; these make up the rest of the
  107. directive line and must be separated from the directive name by
  108. whitespace. For example, &lsquo;<samp>#define</samp>&rsquo; must be followed by a macro
  109. name and the intended expansion of the macro.
  110. </p>
  111. <p>A preprocessing directive cannot cover more than one line. The line
  112. may, however, be continued with backslash-newline, or by a block comment
  113. which extends past the end of the line. In either case, when the
  114. directive is processed, the continuations have already been merged with
  115. the first line to make one long line.
  116. </p>
  117. <hr>
  118. <div class="header">
  119. <p>
  120. Previous: <a href="Tokenization.html" accesskey="p" rel="prev">Tokenization</a>, Up: <a href="Overview.html" 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" title="Index" rel="index">Index</a>]</p>
  121. </div>
  122. </body>
  123. </html>