HOWTO: Use the Token Pasting Operator in Microsoft CLast reviewed: September 2, 1997Article ID: Q51712 |
The information in this article applies to:
SUMMARYIn 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.
MORE INFORMATION
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 "a" from the beginning of the string and "b" from the tail of the string. */ #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. */ #define PRINT_LARGE_SIZE(val) printf("The large size is %d\n",val) #define PRINT_SMALL_SIZE(val) printf("The small size is %d\n",val) #define PRINT(size,val) PRINT_##size##_SIZE(val) void main ( void ) { char *varsrc = "Hello!" ; char *vardest = " " ; /* Example #1: Tokens at beginning and end of string. */ strcpy(GLUE(var,dest),GLUE(var,src)) ; printf("%s\n", 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 = "Hello!" ; char *vardest = " " ; strcpy(vardest,varsrc) ; printf("%s\n", vardest) ; printf("The large size is %d\n",2) ; printf("The small size is %d\n",1) ; }For more information on the token pasting operator, see page 205 of the "Microsoft Visual C++ Reference Volume II: C Language Reference" manual for Visual C++ for Windows, version 1.0 or search the online documentation provided with all versions of Visual C++ for "token-pasting operator" or "##." Keywords : CLngIss kbcode kbfasttip Version : MS-DOS:5.1,6.0,6.00a,6.00ax,7.0; WINDOWS:1.0,1.5; WINDOWS NT:1.0,2.0,2.1,4.0,5.0 Platform : MS-DOS NT WINDOWS Issue type : kbhowto |
================================================================================
© 1998 Microsoft Corporation. All rights reserved. Terms of Use. |