⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 concatenation.html

📁 gcc手册
💻 HTML
字号:
<html lang="en">

<head>

<title>The C Preprocessor</title>

<meta http-equiv="Content-Type" content="text/html">

<meta name="description" content="The C Preprocessor">

<meta name="generator" content="makeinfo 4.3">

<link href="http://www.gnu.org/software/texinfo/" rel="generator-home">

<!--

Copyright &copy; 1987, 1989, 1991, 1992, 1993, 1994, 1995, 1996,

1997, 1998, 1999, 2000, 2001, 2002, 2003

Free Software Foundation, Inc.



   <p>Permission is granted to copy, distribute and/or modify this document

under the terms of the GNU Free Documentation License, Version 1.1 or

any later version published by the Free Software Foundation.  A copy of

the license is included in the

section entitled "GNU Free Documentation License".



   <p>This manual contains no Invariant Sections.  The Front-Cover Texts are

(a) (see below), and the Back-Cover Texts are (b) (see below).



   <p>(a) The FSF's Front-Cover Text is:



   <p>A GNU Manual



   <p>(b) The FSF's Back-Cover Text is:



   <p>You have freedom to copy and modify this GNU Manual, like GNU

     software.  Copies published by the Free Software Foundation raise

     funds for GNU development. 

-->

</head>

<body>

<div class="node">

<p>

Node:<a name="Concatenation">Concatenation</a>,

Next:<a rel="next" accesskey="n" href="Variadic-Macros.html#Variadic%20Macros">Variadic Macros</a>,

Previous:<a rel="previous" accesskey="p" href="Stringification.html#Stringification">Stringification</a>,

Up:<a rel="up" accesskey="u" href="Macros.html#Macros">Macros</a>

<hr><br>

</div>



<h3 class="section">Concatenation</h3>



   <p>It is often useful to merge two tokens into one while expanding macros. 

This is called <dfn>token pasting</dfn> or <dfn>token concatenation</dfn>.  The

<code>##</code> preprocessing operator performs token pasting.  When a macro

is expanded, the two tokens on either side of each <code>##</code> operator

are combined into a single token, which then replaces the <code>##</code> and

the two original tokens in the macro expansion.  Usually both will be

identifiers, or one will be an identifier and the other a preprocessing

number.  When pasted, they make a longer identifier.  This isn't the

only valid case.  It is also possible to concatenate two numbers (or a

number and a name, such as <code>1.5</code> and <code>e3</code>) into a number. 

Also, multi-character operators such as <code>+=</code> can be formed by

token pasting.



   <p>However, two tokens that don't together form a valid token cannot be

pasted together.  For example, you cannot concatenate <code>x</code> with

<code>+</code> in either order.  If you try, the preprocessor issues a warning

and emits the two tokens.  Whether it puts white space between the

tokens is undefined.  It is common to find unnecessary uses of <code>##</code>

in complex macros.  If you get this warning, it is likely that you can

simply remove the <code>##</code>.



   <p>Both the tokens combined by <code>##</code> could come from the macro body,

but you could just as well write them as one token in the first place. 

Token pasting is most useful when one or both of the tokens comes from a

macro argument.  If either of the tokens next to an <code>##</code> is a

parameter name, it is replaced by its actual argument before <code>##</code>

executes.  As with stringification, the actual argument is not

macro-expanded first.  If the argument is empty, that <code>##</code> has no

effect.



   <p>Keep in mind that the C preprocessor converts comments to whitespace

before macros are even considered.  Therefore, you cannot create a

comment by concatenating <code>/</code> and <code>*</code>.  You can put as much

whitespace between <code>##</code> and its operands as you like, including

comments, and you can put comments in arguments that will be

concatenated.  However, it is an error if <code>##</code> appears at either

end of a macro body.



   <p>Consider a C program that interprets named commands.  There probably

needs to be a table of commands, perhaps an array of structures declared

as follows:



<pre class="example">     struct command

     {

       char *name;

       void (*function) (void);

     };

     

     struct command commands[] =

     {

       { "quit", quit_command },

       { "help", help_command },

       ...

     };

     </pre>



   <p>It would be cleaner not to have to give each command name twice, once in

the string constant and once in the function name.  A macro which takes the

name of a command as an argument can make this unnecessary.  The string

constant can be created with stringification, and the function name by

concatenating the argument with <code>_command</code>.  Here is how it is done:



<pre class="example">     #define COMMAND(NAME)  { #NAME, NAME ## _command }

     

     struct command commands[] =

     {

       COMMAND (quit),

       COMMAND (help),

       ...

     };

     </pre>



   </body></html>



⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -