Concatenation.html 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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>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" 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="Macros.html" rel="up" title="Macros">
  30. <link href="Variadic-Macros.html" rel="next" title="Variadic Macros">
  31. <link href="Stringizing.html" rel="prev" title="Stringizing">
  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="Concatenation"></span><div class="header">
  53. <p>
  54. Next: <a href="Variadic-Macros.html" accesskey="n" rel="next">Variadic Macros</a>, Previous: <a href="Stringizing.html" accesskey="p" rel="prev">Stringizing</a>, Up: <a href="Macros.html" 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" title="Index" rel="index">Index</a>]</p>
  55. </div>
  56. <hr>
  57. <span id="Concatenation-1"></span><h3 class="section">3.5 Concatenation</h3>
  58. <span id="index-concatenation"></span>
  59. <span id="index-token-pasting"></span>
  60. <span id="index-token-concatenation"></span>
  61. <span id="index-_0023_0023-operator"></span>
  62. <p>It is often useful to merge two tokens into one while expanding macros.
  63. This is called <em>token pasting</em> or <em>token concatenation</em>. The
  64. &lsquo;<samp>##</samp>&rsquo; preprocessing operator performs token pasting. When a macro
  65. is expanded, the two tokens on either side of each &lsquo;<samp>##</samp>&rsquo; operator
  66. are combined into a single token, which then replaces the &lsquo;<samp>##</samp>&rsquo; and
  67. the two original tokens in the macro expansion. Usually both will be
  68. identifiers, or one will be an identifier and the other a preprocessing
  69. number. When pasted, they make a longer identifier. This isn&rsquo;t the
  70. only valid case. It is also possible to concatenate two numbers (or a
  71. number and a name, such as <code>1.5</code> and <code>e3</code>) into a number.
  72. Also, multi-character operators such as <code>+=</code> can be formed by
  73. token pasting.
  74. </p>
  75. <p>However, two tokens that don&rsquo;t together form a valid token cannot be
  76. pasted together. For example, you cannot concatenate <code>x</code> with
  77. <code>+</code> in either order. If you try, the preprocessor issues a warning
  78. and emits the two tokens. Whether it puts white space between the
  79. tokens is undefined. It is common to find unnecessary uses of &lsquo;<samp>##</samp>&rsquo;
  80. in complex macros. If you get this warning, it is likely that you can
  81. simply remove the &lsquo;<samp>##</samp>&rsquo;.
  82. </p>
  83. <p>Both the tokens combined by &lsquo;<samp>##</samp>&rsquo; could come from the macro body,
  84. but you could just as well write them as one token in the first place.
  85. Token pasting is most useful when one or both of the tokens comes from a
  86. macro argument. If either of the tokens next to an &lsquo;<samp>##</samp>&rsquo; is a
  87. parameter name, it is replaced by its actual argument before &lsquo;<samp>##</samp>&rsquo;
  88. executes. As with stringizing, the actual argument is not
  89. macro-expanded first. If the argument is empty, that &lsquo;<samp>##</samp>&rsquo; has no
  90. effect.
  91. </p>
  92. <p>Keep in mind that the C preprocessor converts comments to whitespace
  93. before macros are even considered. Therefore, you cannot create a
  94. comment by concatenating &lsquo;<samp>/</samp>&rsquo; and &lsquo;<samp>*</samp>&rsquo;. You can put as much
  95. whitespace between &lsquo;<samp>##</samp>&rsquo; and its operands as you like, including
  96. comments, and you can put comments in arguments that will be
  97. concatenated. However, it is an error if &lsquo;<samp>##</samp>&rsquo; appears at either
  98. end of a macro body.
  99. </p>
  100. <p>Consider a C program that interprets named commands. There probably
  101. needs to be a table of commands, perhaps an array of structures declared
  102. as follows:
  103. </p>
  104. <div class="example">
  105. <pre class="example">struct command
  106. {
  107. char *name;
  108. void (*function) (void);
  109. };
  110. </pre><pre class="example">
  111. </pre><pre class="example">struct command commands[] =
  112. {
  113. { &quot;quit&quot;, quit_command },
  114. { &quot;help&quot;, help_command },
  115. &hellip;
  116. };
  117. </pre></div>
  118. <p>It would be cleaner not to have to give each command name twice, once in
  119. the string constant and once in the function name. A macro which takes the
  120. name of a command as an argument can make this unnecessary. The string
  121. constant can be created with stringizing, and the function name by
  122. concatenating the argument with &lsquo;<samp>_command</samp>&rsquo;. Here is how it is done:
  123. </p>
  124. <div class="example">
  125. <pre class="example">#define COMMAND(NAME) { #NAME, NAME ## _command }
  126. struct command commands[] =
  127. {
  128. COMMAND (quit),
  129. COMMAND (help),
  130. &hellip;
  131. };
  132. </pre></div>
  133. <hr>
  134. <div class="header">
  135. <p>
  136. Next: <a href="Variadic-Macros.html" accesskey="n" rel="next">Variadic Macros</a>, Previous: <a href="Stringizing.html" accesskey="p" rel="prev">Stringizing</a>, Up: <a href="Macros.html" 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" title="Index" rel="index">Index</a>]</p>
  137. </div>
  138. </body>
  139. </html>