Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

169 Zeilen
7.4KB

  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-2020 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.5, http://www.gnu.org/software/texinfo/ -->
  18. <head>
  19. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  20. <title>Concatenation (The C Preprocessor)</title>
  21. <meta name="description" content="Concatenation (The C Preprocessor)">
  22. <meta name="keywords" content="Concatenation (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#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="Macros.html#Macros" rel="up" title="Macros">
  30. <link href="Variadic-Macros.html#Variadic-Macros" rel="next" title="Variadic Macros">
  31. <link href="Stringizing.html#Stringizing" rel="prev" title="Stringizing">
  32. <style type="text/css">
  33. <!--
  34. a.summary-letter {text-decoration: none}
  35. blockquote.indentedblock {margin-right: 0em}
  36. blockquote.smallindentedblock {margin-right: 0em; font-size: smaller}
  37. blockquote.smallquotation {font-size: smaller}
  38. div.display {margin-left: 3.2em}
  39. div.example {margin-left: 3.2em}
  40. div.lisp {margin-left: 3.2em}
  41. div.smalldisplay {margin-left: 3.2em}
  42. div.smallexample {margin-left: 3.2em}
  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.nolinebreak {white-space: nowrap}
  54. span.roman {font-family: initial; font-weight: normal}
  55. span.sansserif {font-family: sans-serif; font-weight: normal}
  56. ul.no-bullet {list-style: none}
  57. -->
  58. </style>
  59. </head>
  60. <body lang="en">
  61. <a name="Concatenation"></a>
  62. <div class="header">
  63. <p>
  64. Next: <a href="Variadic-Macros.html#Variadic-Macros" accesskey="n" rel="next">Variadic Macros</a>, Previous: <a href="Stringizing.html#Stringizing" accesskey="p" rel="prev">Stringizing</a>, Up: <a href="Macros.html#Macros" accesskey="u" rel="up">Macros</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>
  65. </div>
  66. <hr>
  67. <a name="Concatenation-1"></a>
  68. <h3 class="section">3.5 Concatenation</h3>
  69. <a name="index-concatenation"></a>
  70. <a name="index-token-pasting"></a>
  71. <a name="index-token-concatenation"></a>
  72. <a name="index-_0023_0023-operator"></a>
  73. <p>It is often useful to merge two tokens into one while expanding macros.
  74. This is called <em>token pasting</em> or <em>token concatenation</em>. The
  75. &lsquo;<samp>##</samp>&rsquo; preprocessing operator performs token pasting. When a macro
  76. is expanded, the two tokens on either side of each &lsquo;<samp>##</samp>&rsquo; operator
  77. are combined into a single token, which then replaces the &lsquo;<samp>##</samp>&rsquo; and
  78. the two original tokens in the macro expansion. Usually both will be
  79. identifiers, or one will be an identifier and the other a preprocessing
  80. number. When pasted, they make a longer identifier. This isn&rsquo;t the
  81. only valid case. It is also possible to concatenate two numbers (or a
  82. number and a name, such as <code>1.5</code> and <code>e3</code>) into a number.
  83. Also, multi-character operators such as <code>+=</code> can be formed by
  84. token pasting.
  85. </p>
  86. <p>However, two tokens that don&rsquo;t together form a valid token cannot be
  87. pasted together. For example, you cannot concatenate <code>x</code> with
  88. <code>+</code> in either order. If you try, the preprocessor issues a warning
  89. and emits the two tokens. Whether it puts white space between the
  90. tokens is undefined. It is common to find unnecessary uses of &lsquo;<samp>##</samp>&rsquo;
  91. in complex macros. If you get this warning, it is likely that you can
  92. simply remove the &lsquo;<samp>##</samp>&rsquo;.
  93. </p>
  94. <p>Both the tokens combined by &lsquo;<samp>##</samp>&rsquo; could come from the macro body,
  95. but you could just as well write them as one token in the first place.
  96. Token pasting is most useful when one or both of the tokens comes from a
  97. macro argument. If either of the tokens next to an &lsquo;<samp>##</samp>&rsquo; is a
  98. parameter name, it is replaced by its actual argument before &lsquo;<samp>##</samp>&rsquo;
  99. executes. As with stringizing, the actual argument is not
  100. macro-expanded first. If the argument is empty, that &lsquo;<samp>##</samp>&rsquo; has no
  101. effect.
  102. </p>
  103. <p>Keep in mind that the C preprocessor converts comments to whitespace
  104. before macros are even considered. Therefore, you cannot create a
  105. comment by concatenating &lsquo;<samp>/</samp>&rsquo; and &lsquo;<samp>*</samp>&rsquo;. You can put as much
  106. whitespace between &lsquo;<samp>##</samp>&rsquo; and its operands as you like, including
  107. comments, and you can put comments in arguments that will be
  108. concatenated. However, it is an error if &lsquo;<samp>##</samp>&rsquo; appears at either
  109. end of a macro body.
  110. </p>
  111. <p>Consider a C program that interprets named commands. There probably
  112. needs to be a table of commands, perhaps an array of structures declared
  113. as follows:
  114. </p>
  115. <div class="smallexample">
  116. <pre class="smallexample">struct command
  117. {
  118. char *name;
  119. void (*function) (void);
  120. };
  121. </pre><pre class="smallexample">
  122. </pre><pre class="smallexample">struct command commands[] =
  123. {
  124. { &quot;quit&quot;, quit_command },
  125. { &quot;help&quot;, help_command },
  126. &hellip;
  127. };
  128. </pre></div>
  129. <p>It would be cleaner not to have to give each command name twice, once in
  130. the string constant and once in the function name. A macro which takes the
  131. name of a command as an argument can make this unnecessary. The string
  132. constant can be created with stringizing, and the function name by
  133. concatenating the argument with &lsquo;<samp>_command</samp>&rsquo;. Here is how it is done:
  134. </p>
  135. <div class="smallexample">
  136. <pre class="smallexample">#define COMMAND(NAME) { #NAME, NAME ## _command }
  137. struct command commands[] =
  138. {
  139. COMMAND (quit),
  140. COMMAND (help),
  141. &hellip;
  142. };
  143. </pre></div>
  144. <hr>
  145. <div class="header">
  146. <p>
  147. Next: <a href="Variadic-Macros.html#Variadic-Macros" accesskey="n" rel="next">Variadic Macros</a>, Previous: <a href="Stringizing.html#Stringizing" accesskey="p" rel="prev">Stringizing</a>, Up: <a href="Macros.html#Macros" accesskey="u" rel="up">Macros</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>
  148. </div>
  149. </body>
  150. </html>