[CPP]宏的字符串处理

来源:互联网 发布:mac散热不好烫手 编辑:程序博客网 时间:2024/05/22 11:46

宏定义无## 和 #, 参数继续展开;否则就不继续展开


1、

#define A(line) shit_##line  

#define B(line) A(line)      
#define C int B(__LINE__) 
   
C; --> int shit_行号;


2、

// 宏定义涉及到字符串连接##,字符串化#, 则参数__LINE__不继续展开
#define B(line) shit_##line
#define C int B(__LINE__)


C; --> int shit___LINE__;


////////////////////////////////////////

3、
#define A holy
#define B shit
#define C(a, b) a##_##b
#define D(a, b) C(a, b)
#define E D(A, B)

E;   -->holy_shit

////////////////////////////////////////


4、

#include <stdio.h> 


#define M 1
#define N 2


#define A(a,b) a##b, a+b 
#define B(a)   #a, a
#define C(a) B(a)   
int main() 
{
char *MN = "here";
    printf("%s, %d, %d\n", C(A(1,2))); --> 12, 1+2, 12, 3
    printf("%s, %d, %d\n", B(A(1,2))); --> A(1,2), 12, 3


printf("%s, %d\n", A(M,N)); --> here, 3
    return 0; 
}


C(a)的宏定义无## 和 #, A(1,2)会被展开成12, 1+2;B(a)宏定义有#,带#的a A(1,2)不会继续展开,不带#的展开成12, 1+2;



0 0
原创粉丝点击