您的位置:首页 > 其它

宏定义的字符串连接##和字符串化#

2014-03-18 18:54 204 查看

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

例:

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;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  行号变量