Microsoft KB Archive/51712

= How To Use the Token Pasting Operator in Microsoft C =

Article ID: 51712

Article Last Modified on 7/5/2005

-

APPLIES TO


 * Microsoft Visual C++ 1.0 Professional Edition
 * Microsoft Visual C++ 1.5 Professional Edition
 * Microsoft Visual C++ 2.0 Professional Edition
 * Microsoft Visual C++ 4.0 Standard Edition
 * Microsoft Visual C++ 5.0 Enterprise Edition
 * Microsoft Visual C++ 6.0 Enterprise Edition
 * Microsoft Visual C++ 5.0 Professional Edition
 * Microsoft Visual C++ 6.0 Professional Edition
 * Microsoft Visual C++ 6.0 Standard Edition

-



This article was previously published under Q51712





SUMMARY
In Microsoft C, the token pasting operator (##) is used to replace tokens within a preprocessor string. The (##) can be used to mark off the first portion of the string as a token, the last part of the string as a token, or the middle of a string as a portion. See the examples shown below.

The token pasting operator cannot be the first or the last token in a preprocessor string. The first example below (GLUE) demonstrates the use of the token pasting operator to concatenate two tokens. There is a replaceable token in front of the operator and one following the operator. The token pasting operator effectively divides the string into the two pieces.

A more complex situation arises when using the token pasting operator to replace a token embedded within a string (for example, THIS_IS_A_size_TOKEN, where size could be replaced with either LARGE or SMALL); the token to be replaced must be surrounded by token pasting operators (##). This is necessary only when the token to be replaced is completely embedded in another string (see example 2). Normally, the space character is used as one of the token markers, while the (##) marks the other end. Only in this specific case (an embedded token) is more than one token pasting operator needed.



Sample Code
/* Compile options needed: none

/*   PASTE.C: Demonstrates the use of the token pasting operator. /*   Example 1 definition. This definition will replace the token &quot;a&quot; from the beginning of the string and &quot;b&quot; from the tail of the string.


 * 1) define GLUE(a,b) a##b

/* - These are the defines for Example 2. Calling print with either LARGE or SMALL will then change the PRINT macro to call the proper PRINT_..._SIZE macro.
 * 1) define PRINT_LARGE_SIZE(val) printf(&quot;The large size is %d\n&quot;,val)
 * 2) define PRINT_SMALL_SIZE(val) printf(&quot;The small size is %d\n&quot;,val)
 * 3) define PRINT(size,val) PRINT_##size##_SIZE(val)
 * 1) define PRINT(size,val) PRINT_##size##_SIZE(val)

void main ( void )

{   char *varsrc = &quot;Hello!&quot; ; char *vardest = &quot;     &quot; ;

/*   Example #1: Tokens at beginning and end of string.

strcpy(GLUE(var,dest),GLUE(var,src)) ; printf(&quot;%s\n&quot;, GLUE(var,dest)) ;

/*   Example #2: Token embedded in string.

PRINT(LARGE, 2) ; /* Calls the printf for large sizes. */    PRINT(SMALL, 1) ; /* Calls the printf for small sizes. */ } The following is the corresponding .i (preprocessed file). This file can be generated by using -P option at compile time (cl -P paste.c). void main ( void )

{   char *varsrc = &quot;Hello!&quot; ; char *vardest = &quot;     &quot; ;

strcpy(vardest,varsrc) ; printf(&quot;%s\n&quot;, vardest) ;

printf(&quot;The large size is %d\n&quot;,2) ; printf(&quot;The small size is %d\n&quot;,1) ; } For more information on the token pasting operator, see page 205 of the &quot;Microsoft Visual C++ Reference Volume II: C Language Reference&quot; manual for Visual C++ for Windows, version 1.0 or search the online documentation provided with all versions of Visual C++ for &quot;token-pasting operator&quot; or &quot;##.&quot;

Keywords: kbhowto kblangc kbcode KB51712

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.