宏定义中的#,##操作符和... and _ _VA_ARGS_ _ - [C/C++]
来源:互联网 发布:java 单态模式 编辑:程序博客网 时间:2024/04/28 21:31
1.#
假如希望在字符串中包含宏参数,ANSI C允许这样作,在类函数宏的替换部分,#符号用作一个预处理运算符,它可以把语言符号转化程字符串。例如,如果x是一个宏参量,那么#x可以把参数名转化成相应的字符串。该过程称为字符串化(stringizing).
#incldue <stdio.h>#define PSQR(x) printf("the square of" #x "is %d.\n",(x)*(x))int main(void){ int y =4; PSQR(y); PSQR(2+4); return 0;}输出结果:
the square of y is 16.
the square of 2+4 is 36.
第一次调用宏时使用“y”代替#x;第二次调用时用“2+4"代#x。
2.##
##运算符可以使用类函数宏的替换部分。另外,##还可以用于类对象宏的替换部分。这个运算符把两个语言符号组合成单个语言符号。例如:
#define XNAME(n) x##n
这样宏调用:
XNAME(4)
展开后:
x4
程序:
#include <stdio.h>#define XNAME(x) x##4#define PXN(n) printf("x"#n" = %d\n",x##n)int main(void){ int XNAME(1)=12;//int x1=12; PXN(1);//printf("x1 = %d\n", x1); return 0;}3.可变宏 ...和_ _VA_ARGS_ _
实现思想就是宏定义中参数列表的最后一个参数为省略号(也就是三个点)。这样预定义宏_ _VA_ARGS_ _就可以被用在替换部分中,以表示省略号代表什么。比如:
#define PR(...) printf(_ _VA_ARGS_ _)
PR("hello");-->printf("hello");
PR("weight = %d, shipping = $.2f",wt,sp);
-->printf("weight = %d, shipping = $.2f",wt,sp);
省略号只能代替最后面的宏参数。
#define W(x,...,y)错误!
// variadic.c -- variadic macros
#include <stdio.h>#include <math.h>#define PR(X, ...) printf("Message" #X ": " _ _VA_ARGS_ _)int main(void){ double x = 48; double y; y = sqrt(x); PR(1, "x = %g\n", x); PR(2, "x = %.2f, y = %.4f\n", x, y); return 0;}
In the first macro call, X has the value 1, so #X becomes "1". That makes the expansion look like this:
(#为参数加双引号。)
print("Message " "1" ": " "x = %g\n", x);
Then the four strings are concatenated, reducing the call to this:
print("Message 1: x = %g\n", x);
Here's the output:
Message 1: x = 48
Message 2: x = 48.00, y = 6.9282
Don't forget, the ellipses have to be the last macro argument:
#define WRONG(X, ..., Y) #X #_ _VA_ARGS_ _ #y(这个是错误的例子。)
【本文转自:http://hi.baidu.com/zifengshen/blog/item/01950f23f3da72599822ed0f.html】
- 宏定义中的#,##操作符和... and _ _VA_ARGS_ _ - [C/C++]
- 宏定义中的#,##操作符和... and _ _VA_ARGS_ _
- 宏定义中的#,##操作符和... and _ _VA_ARGS_ _
- 宏定义中的#,##操作符和... and _ _VA_ARGS_ _
- 宏定义中的##操作符和... and _ _VA_ARGS_ _
- 宏定义中的##操作符和... and _ _VA_ARGS_ _
- 宏定义中的##操作符和... and _ _VA_ARGS_ _ .
- 宏定义中的##操作符和... and _ _VA_ARGS_ _
- 宏定义中的##操作符和... and _ _VA_ARGS_ _
- 宏定义中的##操作符和... and _ _VA_ARGS_ _
- 宏定义中的 # , ##以及 _ _VA_ARGS_ _ 的作用
- 宏 #,##,_ _VA_ARGS_ _
- # ## _ _VA_ARGS_ _宏相关
- # ## _ _VA_ARGS_ _宏相关
- ##、“...”、_ _VA_ARGS_ _
- C语言_初成长_操作符和表达式
- c++_操作符重载
- 如何使用# ## ... _ _VA_ARGS_ _
- Oracle导入导出数据库的语法
- 记录
- 美科学家称黑洞不存在 其实是暗能量星
- CGI
- asp.net导出txt文件
- 宏定义中的#,##操作符和... and _ _VA_ARGS_ _ - [C/C++]
- Nginx 全局变量
- android 源码下载镜像
- 好文
- 日历 农历算法
- 列表推导式
- [回顾]2011年九大热门语言发展趋势
- Linux简单的不常用的有用的命令设置等
- 一些几何算法