PRB: Concatenation Symbol (##) Yields Errors

Article ID: Q116449

The information in this article applies to:
  • The Microsoft C/C++ Compiler (CL.EXE) included with: - Microsoft C for MS-DOS, versions 6.0, 6.0a, 6.0ax - Microsoft C/C++ for MS-DOS, version 7.0 - Microsoft Visual C++ for Windows, versions 1.0, 1.5, 1.51 - Microsoft Visual C++ 32-bit Edition, versions 1.0, 2.0, 2.1, 2.2, 4.0,

         4.1, 4.2, 5.0
    

SYMPTOMS

The ## preprocessing macro concatenates the arguments on either side of the "##". However, as stated in the "ANSI Specification," the resultant value must be a preprocessing token. If it is not, the compiler generates the following error messages:

   C2065: 'token' : undefined
   C2146: syntax error : missing ')' before identifier 'token1'

CAUSE

This is by design. The "ANSI Specification," section 3.8.3.3, page 91, states, regarding ##, that "If the result is not a valid preprocessing token, the behavior is undefined."

These error messages are generated unless one of two things happens:

  • The result of the concatenation is a preprocessing token.

    -or-

  • The compiler option /E is used, and the resulting source file is run through the compiler:

          cl /E test.c > test1.c
          cl test1.c
    

MORE INFORMATION

You can use the sample code below to demonstrate how these error messages are generated. The intention of the code is to construct the text string "_fheapchk" by concatenating "_f" and "heapchk". Because _fheapchk is not a preprocessor token, the error occurs unless you use the /E option.

NOTE: This example doesn't make much sense for 32-bit code, but it does illustrate the kind of problem this article is talking about.

Sample Code

   /* Compile options needed: none
   */ 

   #define DEFAULTMODEL() _f

   #define FUNCNAME_NA(prefix,func) prefix##func( )

   void checkne_func(int a)
   {
   }

   void main(void)
   {
      checkne_func((int)(FUNCNAME_NA(DEFAULTMODEL(),heapchk)));
   }

Additional query words: 8.00 8.00c 9.00 9.10 10.00 10.10
Keywords          : kbCompiler 
Version           : MS-DOS:6.0,6.00a,6.00ax,7.0;WINDOWS:1.0,1.5,1.51 WINDOWS  NT:1.0,2.0,2.1,2.2,4.0,4.1,4.2,5.0
Platform          : MS-DOS NT WINDOWS
Issue type        : kbprb


Last Reviewed: August 28, 1997
© 1999 Microsoft Corporation. All rights reserved. Terms of Use.