KnowledgeBase Archive

An Archive of Early Microsoft KnowledgeBase Articles

View on GitHub

Q51712: HOWTO: Use the Token Pasting Operator in Microsoft C

Article: Q51712
Product(s): Microsoft C Compiler
Version(s): MS-DOS:; WINDOWS:1.0,1.5; winnt:1.0,2.0,4.0,5.0,6.0
Operating System(s): 
Keyword(s): kbcode kbLangC kbVC100 kbVC150 kbVC200 kbVC210 kbVC400 kbVC500 kbVC600
Last Modified: 11-DEC-2001

-------------------------------------------------------------------------------
The information in this article applies to:

- Microsoft C/C++ for MS-DOS 
- Microsoft Visual C++, versions 1.0, 1.5, 2.0, 4.0 
- Microsoft Visual C++, 32-bit Enterprise Edition, versions 5.0, 6.0 
- Microsoft Visual C++, 32-bit Professional Edition, versions 5.0, 6.0 
- Microsoft Visual C++, 32-bit Learning Edition, version 6.0 
-------------------------------------------------------------------------------

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.

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 "##."

Additional query words:

======================================================================
Keywords          : kbcode kbLangC kbVC100 kbVC150 kbVC200 kbVC210 kbVC400 kbVC500 kbVC600 
Technology        : kbVCsearch kbVC400 kbAudDeveloper kbZNotKeyword8 kbvc150 kbvc100 kbZNotKeyword3 kbVC500 kbVC600 kbVC200 kbVC32bitSearch kbVC500Search
Version           : MS-DOS:; WINDOWS:1.0,1.5; winnt:1.0,2.0,4.0,5.0,6.0
Issue type        : kbhowto

=============================================================================

THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Copyright Microsoft Corporation 1986-2002.