One thing to be aware of when you're using the token-paste ('##
') or stringizing ('#
') preprocessing operators is that you have to use an extra level of indirection for them to work properly in all cases.
If you don't do this and the items passed to the token-pasting operator are macros themselves, you'll get results that are probably not what you want:
#include <stdio.h>
#define STRINGIFY2( x) #x
#define STRINGIFY(x) STRINGIFY2(x)
#define PASTE2( a, b) a##b
#define PASTE( a, b) PASTE2( a, b)
#define BAD_PASTE(x,y) x##y
#define BAD_STRINGIFY(x) #x
#define SOME_MACRO function_name
int main()
{
printf( "buggy results:
");
printf( "%s
", STRINGIFY( BAD_PASTE( SOME_MACRO, __LINE__)));
printf( "%s
", BAD_STRINGIFY( BAD_PASTE( SOME_MACRO, __LINE__)));
printf( "%s
", BAD_STRINGIFY( PASTE( SOME_MACRO, __LINE__)));
printf( "
" "desired result:
");
printf( "%s
", STRINGIFY( PASTE( SOME_MACRO, __LINE__)));
}
The output:
buggy results:
SOME_MACRO__LINE__
BAD_PASTE( SOME_MACRO, __LINE__)
PASTE( SOME_MACRO, __LINE__)
desired result:
function_name21
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…