1 | <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
|
---|
2 | <html>
|
---|
3 | <!-- Copyright (C) 2022 Richard Stallman and Free Software Foundation, Inc.
|
---|
4 |
|
---|
5 | (The work of Trevis Rothwell and Nelson Beebe has been assigned or
|
---|
6 | licensed to the FSF.)
|
---|
7 |
|
---|
8 | Permission is granted to copy, distribute and/or modify this document
|
---|
9 | under the terms of the GNU Free Documentation License, Version 1.3 or
|
---|
10 | any later version published by the Free Software Foundation; with the
|
---|
11 | Invariant Sections being "GNU General Public License," with the
|
---|
12 | Front-Cover Texts being "A GNU Manual," and with the Back-Cover
|
---|
13 | Texts as in (a) below. A copy of the license is included in the
|
---|
14 | section entitled "GNU Free Documentation License."
|
---|
15 |
|
---|
16 | (a) The FSF's Back-Cover Text is: "You have the freedom to copy and
|
---|
17 | modify this GNU manual. Buying copies from the FSF supports it in
|
---|
18 | developing GNU and promoting software freedom." -->
|
---|
19 | <!-- Created by GNU Texinfo 6.7, http://www.gnu.org/software/texinfo/ -->
|
---|
20 | <head>
|
---|
21 | <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
|
---|
22 | <title>Concatenation (GNU C Language Manual)</title>
|
---|
23 |
|
---|
24 | <meta name="description" content="Concatenation (GNU C Language Manual)">
|
---|
25 | <meta name="keywords" content="Concatenation (GNU C Language Manual)">
|
---|
26 | <meta name="resource-type" content="document">
|
---|
27 | <meta name="distribution" content="global">
|
---|
28 | <meta name="Generator" content="makeinfo">
|
---|
29 | <link href="index.html" rel="start" title="Top">
|
---|
30 | <link href="Symbol-Index.html" rel="index" title="Symbol Index">
|
---|
31 | <link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
|
---|
32 | <link href="Macros.html" rel="up" title="Macros">
|
---|
33 | <link href="Variadic-Macros.html" rel="next" title="Variadic Macros">
|
---|
34 | <link href="Stringification.html" rel="prev" title="Stringification">
|
---|
35 | <style type="text/css">
|
---|
36 | <!--
|
---|
37 | a.summary-letter {text-decoration: none}
|
---|
38 | blockquote.indentedblock {margin-right: 0em}
|
---|
39 | div.display {margin-left: 3.2em}
|
---|
40 | div.example {margin-left: 3.2em}
|
---|
41 | div.lisp {margin-left: 3.2em}
|
---|
42 | kbd {font-style: oblique}
|
---|
43 | pre.display {font-family: inherit}
|
---|
44 | pre.format {font-family: inherit}
|
---|
45 | pre.menu-comment {font-family: serif}
|
---|
46 | pre.menu-preformatted {font-family: serif}
|
---|
47 | span.nolinebreak {white-space: nowrap}
|
---|
48 | span.roman {font-family: initial; font-weight: normal}
|
---|
49 | span.sansserif {font-family: sans-serif; font-weight: normal}
|
---|
50 | ul.no-bullet {list-style: none}
|
---|
51 | -->
|
---|
52 | </style>
|
---|
53 |
|
---|
54 |
|
---|
55 | </head>
|
---|
56 |
|
---|
57 | <body lang="en">
|
---|
58 | <span id="Concatenation"></span><div class="header">
|
---|
59 | <p>
|
---|
60 | Next: <a href="Variadic-Macros.html" accesskey="n" rel="next">Variadic Macros</a>, Previous: <a href="Stringification.html" accesskey="p" rel="prev">Stringification</a>, Up: <a href="Macros.html" accesskey="u" rel="up">Macros</a> [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Symbol-Index.html" title="Index" rel="index">Index</a>]</p>
|
---|
61 | </div>
|
---|
62 | <hr>
|
---|
63 | <span id="Concatenation-1"></span><h4 class="subsection">26.5.5 Concatenation</h4>
|
---|
64 | <span id="index-concatenation"></span>
|
---|
65 | <span id="index-token-pasting"></span>
|
---|
66 | <span id="index-token-concatenation"></span>
|
---|
67 | <span id="index-_0023_0023-operator"></span>
|
---|
68 |
|
---|
69 | <p>It is often useful to merge two tokens into one while expanding macros.
|
---|
70 | This is called <em>token pasting</em> or <em>token concatenation</em>. The
|
---|
71 | <code>##</code> preprocessing operator performs token pasting. When a macro
|
---|
72 | is expanded, the two tokens on either side of each <code>##</code> operator
|
---|
73 | are combined into a single token, which then replaces the <code>##</code> and
|
---|
74 | the two original tokens in the macro expansion. Usually both will be
|
---|
75 | identifiers, or one will be an identifier and the other a preprocessing
|
---|
76 | number. When pasted, they make a longer identifier.
|
---|
77 | </p>
|
---|
78 | <p>Concatenation into an identifier isn’t the only valid case. It is
|
---|
79 | also possible to concatenate two numbers (or a number and a name, such
|
---|
80 | as <code>1.5</code> and <code>e3</code>) into a number. Also, multi-character
|
---|
81 | operators such as <code>+=</code> can be formed by token pasting.
|
---|
82 | </p>
|
---|
83 | <p>However, two tokens that don’t together form a valid token cannot be
|
---|
84 | pasted together. For example, you cannot concatenate <code>x</code> with
|
---|
85 | <code>+</code>, not in either order. Trying this issues a warning and keeps
|
---|
86 | the two tokens separate. Whether it puts white space between the
|
---|
87 | tokens is undefined. It is common to find unnecessary uses of
|
---|
88 | <code>##</code> in complex macros. If you get this warning, it is likely
|
---|
89 | that you can simply remove the <code>##</code>.
|
---|
90 | </p>
|
---|
91 | <p>The tokens combined by <code>##</code> could both come from the macro body,
|
---|
92 | but then you could just as well write them as one token in the first place.
|
---|
93 | Token pasting is useful when one or both of the tokens comes from a
|
---|
94 | macro argument. If either of the tokens next to an <code>##</code> is a
|
---|
95 | parameter name, it is replaced by its actual argument before <code>##</code>
|
---|
96 | executes. As with stringification, the actual argument is not
|
---|
97 | macro-expanded first. If the argument is empty, that <code>##</code> has no
|
---|
98 | effect.
|
---|
99 | </p>
|
---|
100 | <p>Keep in mind that preprocessing converts comments to whitespace before
|
---|
101 | it looks for uses of macros. Therefore, you cannot create a comment
|
---|
102 | by concatenating ‘<samp>/</samp>’ and ‘<samp>*</samp>’. You can put as much
|
---|
103 | whitespace between <code>##</code> and its operands as you like, including
|
---|
104 | comments, and you can put comments in arguments that will be
|
---|
105 | concatenated.
|
---|
106 | </p>
|
---|
107 | <p>It is an error to use <code>##</code> at the beginning or end of a macro
|
---|
108 | body.
|
---|
109 | </p>
|
---|
110 | <p>Multiple <code>##</code> operators are handled left-to-right, so that
|
---|
111 | ‘<samp>1 ## e ## -2</samp>’ pastes into ‘<samp>1e-2</samp>’. (Right-to-left
|
---|
112 | processing would first generate ‘<samp>e-2</samp>’, which is an invalid token.)
|
---|
113 | When <code>#</code> and <code>##</code> are used together, they are all handled
|
---|
114 | left-to-right.
|
---|
115 | </p>
|
---|
116 | <p>Consider a C program that interprets named commands. There probably
|
---|
117 | needs to be a table of commands, perhaps an array of structures declared
|
---|
118 | as follows:
|
---|
119 | </p>
|
---|
120 | <div class="example">
|
---|
121 | <pre class="example">struct command
|
---|
122 | {
|
---|
123 | char *name;
|
---|
124 | void (*function) (void);
|
---|
125 | };
|
---|
126 | </pre><pre class="example">
|
---|
127 |
|
---|
128 | </pre><pre class="example">struct command commands[] =
|
---|
129 | {
|
---|
130 | { "quit", quit_command },
|
---|
131 | { "help", help_command },
|
---|
132 | /* <span class="roman">…</span> */
|
---|
133 | };
|
---|
134 | </pre></div>
|
---|
135 |
|
---|
136 | <p>It would be cleaner not to have to write each command name twice, once
|
---|
137 | in the string constant and once in the function name. A macro that
|
---|
138 | takes the name of a command as an argument can make this unnecessary.
|
---|
139 | It can create the string constant with stringification, and the
|
---|
140 | function name by concatenating the argument with ‘<samp>_command</samp>’.
|
---|
141 | Here is how it is done:
|
---|
142 | </p>
|
---|
143 | <div class="example">
|
---|
144 | <pre class="example">#define COMMAND(NAME) { #NAME, NAME ## _command }
|
---|
145 |
|
---|
146 | struct command commands[] =
|
---|
147 | {
|
---|
148 | COMMAND (quit),
|
---|
149 | COMMAND (help),
|
---|
150 | /* <span class="roman">…</span> */
|
---|
151 | };
|
---|
152 | </pre></div>
|
---|
153 |
|
---|
154 | <hr>
|
---|
155 | <div class="header">
|
---|
156 | <p>
|
---|
157 | Next: <a href="Variadic-Macros.html" accesskey="n" rel="next">Variadic Macros</a>, Previous: <a href="Stringification.html" accesskey="p" rel="prev">Stringification</a>, Up: <a href="Macros.html" accesskey="u" rel="up">Macros</a> [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Symbol-Index.html" title="Index" rel="index">Index</a>]</p>
|
---|
158 | </div>
|
---|
159 |
|
---|
160 |
|
---|
161 |
|
---|
162 | </body>
|
---|
163 | </html>
|
---|