• Home
  • History
  • Annotate
  • Line#
  • Navigate
  • Raw
  • Download
  • only in /asuswrt-rt-n18u-9.0.0.4.380.2695/release/src-rt-6.x.4708/toolchains/hndtools-armeabi-2011.09/share/doc/arm-arm-none-eabi/html/cpp/
1<html lang="en">
2<head>
3<title>Concatenation - The C Preprocessor</title>
4<meta http-equiv="Content-Type" content="text/html">
5<meta name="description" content="The C Preprocessor">
6<meta name="generator" content="makeinfo 4.13">
7<link title="Top" rel="start" href="index.html#Top">
8<link rel="up" href="Macros.html#Macros" title="Macros">
9<link rel="prev" href="Stringification.html#Stringification" title="Stringification">
10<link rel="next" href="Variadic-Macros.html#Variadic-Macros" title="Variadic Macros">
11<link href="http://www.gnu.org/software/texinfo/" rel="generator-home" title="Texinfo Homepage">
12<!--
13Copyright (C) 1987, 1989, 1991, 1992, 1993, 1994, 1995, 1996,
141997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007,
152008, 2009, 2010, 2011
16Free Software Foundation, Inc.
17
18Permission is granted to copy, distribute and/or modify this document
19under the terms of the GNU Free Documentation License, Version 1.3 or
20any later version published by the Free Software Foundation.  A copy of
21the license is included in the
22section entitled ``GNU Free Documentation License''.
23
24This manual contains no Invariant Sections.  The Front-Cover Texts are
25(a) (see below), and the Back-Cover Texts are (b) (see below).
26
27(a) The FSF's Front-Cover Text is:
28
29     A GNU Manual
30
31(b) The FSF's Back-Cover Text is:
32
33     You have freedom to copy and modify this GNU Manual, like GNU
34     software.  Copies published by the Free Software Foundation raise
35     funds for GNU development.
36-->
37<meta http-equiv="Content-Style-Type" content="text/css">
38<style type="text/css"><!--
39  pre.display { font-family:inherit }
40  pre.format  { font-family:inherit }
41  pre.smalldisplay { font-family:inherit; font-size:smaller }
42  pre.smallformat  { font-family:inherit; font-size:smaller }
43  pre.smallexample { font-size:smaller }
44  pre.smalllisp    { font-size:smaller }
45  span.sc    { font-variant:small-caps }
46  span.roman { font-family:serif; font-weight:normal; } 
47  span.sansserif { font-family:sans-serif; font-weight:normal; } 
48--></style>
49<link rel="stylesheet" type="text/css" href="../cs.css">
50</head>
51<body>
52<div class="node">
53<a name="Concatenation"></a>
54<p>
55Next:&nbsp;<a rel="next" accesskey="n" href="Variadic-Macros.html#Variadic-Macros">Variadic Macros</a>,
56Previous:&nbsp;<a rel="previous" accesskey="p" href="Stringification.html#Stringification">Stringification</a>,
57Up:&nbsp;<a rel="up" accesskey="u" href="Macros.html#Macros">Macros</a>
58<hr>
59</div>
60
61<h3 class="section">3.5 Concatenation</h3>
62
63<p><a name="index-concatenation-52"></a><a name="index-token-pasting-53"></a><a name="index-token-concatenation-54"></a><a name="index-g_t_0040samp_007b_0023_0023_007d-operator-55"></a>
64It is often useful to merge two tokens into one while expanding macros. 
65This is called <dfn>token pasting</dfn> or <dfn>token concatenation</dfn>.  The
66&lsquo;<samp><span class="samp">##</span></samp>&rsquo; preprocessing operator performs token pasting.  When a macro
67is expanded, the two tokens on either side of each &lsquo;<samp><span class="samp">##</span></samp>&rsquo; operator
68are combined into a single token, which then replaces the &lsquo;<samp><span class="samp">##</span></samp>&rsquo; and
69the two original tokens in the macro expansion.  Usually both will be
70identifiers, or one will be an identifier and the other a preprocessing
71number.  When pasted, they make a longer identifier.  This isn't the
72only valid case.  It is also possible to concatenate two numbers (or a
73number and a name, such as <code>1.5</code> and <code>e3</code>) into a number. 
74Also, multi-character operators such as <code>+=</code> can be formed by
75token pasting.
76
77   <p>However, two tokens that don't together form a valid token cannot be
78pasted together.  For example, you cannot concatenate <code>x</code> with
79<code>+</code> in either order.  If you try, the preprocessor issues a warning
80and emits the two tokens.  Whether it puts white space between the
81tokens is undefined.  It is common to find unnecessary uses of &lsquo;<samp><span class="samp">##</span></samp>&rsquo;
82in complex macros.  If you get this warning, it is likely that you can
83simply remove the &lsquo;<samp><span class="samp">##</span></samp>&rsquo;.
84
85   <p>Both the tokens combined by &lsquo;<samp><span class="samp">##</span></samp>&rsquo; could come from the macro body,
86but you could just as well write them as one token in the first place. 
87Token pasting is most useful when one or both of the tokens comes from a
88macro argument.  If either of the tokens next to an &lsquo;<samp><span class="samp">##</span></samp>&rsquo; is a
89parameter name, it is replaced by its actual argument before &lsquo;<samp><span class="samp">##</span></samp>&rsquo;
90executes.  As with stringification, the actual argument is not
91macro-expanded first.  If the argument is empty, that &lsquo;<samp><span class="samp">##</span></samp>&rsquo; has no
92effect.
93
94   <p>Keep in mind that the C preprocessor converts comments to whitespace
95before macros are even considered.  Therefore, you cannot create a
96comment by concatenating &lsquo;<samp><span class="samp">/</span></samp>&rsquo; and &lsquo;<samp><span class="samp">*</span></samp>&rsquo;.  You can put as much
97whitespace between &lsquo;<samp><span class="samp">##</span></samp>&rsquo; and its operands as you like, including
98comments, and you can put comments in arguments that will be
99concatenated.  However, it is an error if &lsquo;<samp><span class="samp">##</span></samp>&rsquo; appears at either
100end of a macro body.
101
102   <p>Consider a C program that interprets named commands.  There probably
103needs to be a table of commands, perhaps an array of structures declared
104as follows:
105
106<pre class="smallexample">     struct command
107     {
108       char *name;
109       void (*function) (void);
110     };
111     
112     struct command commands[] =
113     {
114       { "quit", quit_command },
115       { "help", help_command },
116       ...
117     };
118</pre>
119   <p>It would be cleaner not to have to give each command name twice, once in
120the string constant and once in the function name.  A macro which takes the
121name of a command as an argument can make this unnecessary.  The string
122constant can be created with stringification, and the function name by
123concatenating the argument with &lsquo;<samp><span class="samp">_command</span></samp>&rsquo;.  Here is how it is done:
124
125<pre class="smallexample">     #define COMMAND(NAME)  { #NAME, NAME ## _command }
126     
127     struct command commands[] =
128     {
129       COMMAND (quit),
130       COMMAND (help),
131       ...
132     };
133</pre>
134   </body></html>
135
136