source: public/doc/gnu-c/Concatenation.html@ 02598c2

Last change on this file since 02598c2 was 02598c2, checked in by Mikhail Kirillov <w96k@…>, on Oct 6, 2022 at 12:36:29 PM

Add gnu-c

  • Property mode set to 100644
File size: 7.2 KB
Line 
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
6licensed to the FSF.)
7
8Permission is granted to copy, distribute and/or modify this document
9under the terms of the GNU Free Documentation License, Version 1.3 or
10any later version published by the Free Software Foundation; with the
11Invariant Sections being "GNU General Public License," with the
12Front-Cover Texts being "A GNU Manual," and with the Back-Cover
13Texts as in (a) below. A copy of the license is included in the
14section entitled "GNU Free Documentation License."
15
16(a) The FSF's Back-Cover Text is: "You have the freedom to copy and
17modify this GNU manual. Buying copies from the FSF supports it in
18developing 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<!--
37a.summary-letter {text-decoration: none}
38blockquote.indentedblock {margin-right: 0em}
39div.display {margin-left: 3.2em}
40div.example {margin-left: 3.2em}
41div.lisp {margin-left: 3.2em}
42kbd {font-style: oblique}
43pre.display {font-family: inherit}
44pre.format {font-family: inherit}
45pre.menu-comment {font-family: serif}
46pre.menu-preformatted {font-family: serif}
47span.nolinebreak {white-space: nowrap}
48span.roman {font-family: initial; font-weight: normal}
49span.sansserif {font-family: sans-serif; font-weight: normal}
50ul.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>
60Next: <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> &nbsp; [<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.
70This is called <em>token pasting</em> or <em>token concatenation</em>. The
71<code>##</code> preprocessing operator performs token pasting. When a macro
72is expanded, the two tokens on either side of each <code>##</code> operator
73are combined into a single token, which then replaces the <code>##</code> and
74the two original tokens in the macro expansion. Usually both will be
75identifiers, or one will be an identifier and the other a preprocessing
76number. When pasted, they make a longer identifier.
77</p>
78<p>Concatenation into an identifier isn&rsquo;t the only valid case. It is
79also possible to concatenate two numbers (or a number and a name, such
80as <code>1.5</code> and <code>e3</code>) into a number. Also, multi-character
81operators such as <code>+=</code> can be formed by token pasting.
82</p>
83<p>However, two tokens that don&rsquo;t together form a valid token cannot be
84pasted together. For example, you cannot concatenate <code>x</code> with
85<code>+</code>, not in either order. Trying this issues a warning and keeps
86the two tokens separate. Whether it puts white space between the
87tokens is undefined. It is common to find unnecessary uses of
88<code>##</code> in complex macros. If you get this warning, it is likely
89that you can simply remove the <code>##</code>.
90</p>
91<p>The tokens combined by <code>##</code> could both come from the macro body,
92but then you could just as well write them as one token in the first place.
93Token pasting is useful when one or both of the tokens comes from a
94macro argument. If either of the tokens next to an <code>##</code> is a
95parameter name, it is replaced by its actual argument before <code>##</code>
96executes. As with stringification, the actual argument is not
97macro-expanded first. If the argument is empty, that <code>##</code> has no
98effect.
99</p>
100<p>Keep in mind that preprocessing converts comments to whitespace before
101it looks for uses of macros. Therefore, you cannot create a comment
102by concatenating &lsquo;<samp>/</samp>&rsquo; and &lsquo;<samp>*</samp>&rsquo;. You can put as much
103whitespace between <code>##</code> and its operands as you like, including
104comments, and you can put comments in arguments that will be
105concatenated.
106</p>
107<p>It is an error to use <code>##</code> at the beginning or end of a macro
108body.
109</p>
110<p>Multiple <code>##</code> operators are handled left-to-right, so that
111&lsquo;<samp>1 ## e ## -2</samp>&rsquo; pastes into &lsquo;<samp>1e-2</samp>&rsquo;. (Right-to-left
112processing would first generate &lsquo;<samp>e-2</samp>&rsquo;, which is an invalid token.)
113When <code>#</code> and <code>##</code> are used together, they are all handled
114left-to-right.
115</p>
116<p>Consider a C program that interprets named commands. There probably
117needs to be a table of commands, perhaps an array of structures declared
118as 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 { &quot;quit&quot;, quit_command },
131 { &quot;help&quot;, help_command },
132 /* <span class="roman">&hellip;</span> */
133};
134</pre></div>
135
136<p>It would be cleaner not to have to write each command name twice, once
137in the string constant and once in the function name. A macro that
138takes the name of a command as an argument can make this unnecessary.
139It can create the string constant with stringification, and the
140function name by concatenating the argument with &lsquo;<samp>_command</samp>&rsquo;.
141Here is how it is done:
142</p>
143<div class="example">
144<pre class="example">#define COMMAND(NAME) { #NAME, NAME ## _command }
145
146struct command commands[] =
147{
148 COMMAND (quit),
149 COMMAND (help),
150 /* <span class="roman">&hellip;</span> */
151};
152</pre></div>
153
154<hr>
155<div class="header">
156<p>
157Next: <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> &nbsp; [<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>
Note: See TracBrowser for help on using the repository browser.