C/C++ 宏定义中#、##、#@的区别
2017-11-22 11:20
204 查看
#表示:对应变量字符串化
##表示:把宏参数名与宏定义代码序列中的标识符连接在一起,形成一个新的标识符
连接符#@:它将单字符标记符变换为单字符,即加单引号。例如:
#define B(x) #@x
则B(a)即'a',B(1)即'1',但B(abc)却不甚有效。
#include <stdio.h>
#define trace(x, format) printf(#x " = %" #format "\n", x)
#define trace2(i) trace(x##i, d)
int _tmain(int argc, _TCHAR* argv[])
{
int i = 1;
char *s = "three";
float x = 2.0;
trace(i, d); // 相当于 printf("x = %d\n", x)
trace(x, f); // 相当于 printf("x = %f\n", x)
trace(s, s); // 相当于 printf("x = %s\n", x)
int x1 = 1, x2 = 2, x3 = 3;
trace2(1); // 相当于 trace(x1, d)
trace2(2); // 相当于 trace(x2, d)
trace2(3); // 相当于 trace(x3, d)
return 0;
}
输出结果:
i = 1
x = 2.000000
s = three
x1 = 1
x2 = 2
x3 = 3
##表示:把宏参数名与宏定义代码序列中的标识符连接在一起,形成一个新的标识符
连接符#@:它将单字符标记符变换为单字符,即加单引号。例如:
#define B(x) #@x
则B(a)即'a',B(1)即'1',但B(abc)却不甚有效。
#include <stdio.h>
#define trace(x, format) printf(#x " = %" #format "\n", x)
#define trace2(i) trace(x##i, d)
int _tmain(int argc, _TCHAR* argv[])
{
int i = 1;
char *s = "three";
float x = 2.0;
trace(i, d); // 相当于 printf("x = %d\n", x)
trace(x, f); // 相当于 printf("x = %f\n", x)
trace(s, s); // 相当于 printf("x = %s\n", x)
int x1 = 1, x2 = 2, x3 = 3;
trace2(1); // 相当于 trace(x1, d)
trace2(2); // 相当于 trace(x2, d)
trace2(3); // 相当于 trace(x3, d)
return 0;
}
输出结果:
i = 1
x = 2.000000
s = three
x1 = 1
x2 = 2
x3 = 3
相关文章推荐
- [C++基础]宏定义、内联函数、普通函数的区别
- c++: C/C++ 宏定义中#、##、#@的区别
- C++中宏定义和内联函数区别
- C++中的内联函数和C中的宏定义的区别
- C/C++: const常量与define宏定义的区别
- C/C++宏定义中#与##区别
- C/C++宏定义中#与##区别
- C++中,内联函数和宏定义的区别是什么?
- C/C++ 宏定义中#、##、#@的区别
- c++中内敛函数与宏定义的区别
- c++中const、内联函数与c语言中的宏定义的区别
- C++:为什么inline函数的定义要放在头文件里 + inline和宏定义的区别(暂未体会到)
- C/C++宏定义中#与##区别
- C++中,内联函数和宏定义的区别是什么?
- C/C++中宏定义和常变量的区别
- C++内联函数与宏定义的区别
- C/C++宏定义中#与##区别 .
- C++基础:#define宏定义和inline,const,enum的区别
- C++ 宏定义和 typedef 的区别
- 【转】【c/c++】define宏定义和const常量定义之间的区别