转自 http://blog.csdn.net/todd911/article/details/8943149
方法一:
[cpp] view plaincopy
- #include <stdio.h>
- #define PRINT(FORMAT,VALUE) \
- printf("the value is "FORMAT"\n",VALUE);
- int main(void){
- int x = 6;
- PRINT("%d",x+2);
- return 0;
- }
运行结果:
the value is 8
方法二:
[cpp] view plaincopy
- #include <stdio.h>
- #define PRINT(FORMAT,VALUE) \
- printf("the value of "#VALUE" is "FORMAT"\n",VALUE);
- int main(void){
- int x = 6;
- PRINT("%d",x+2);
- return 0;
- }
运行结果:
the value of x+2 is 8
代码中的#将变量转换成了字符串后输出。
最后再介绍一种#号的用法:
[cpp] view plaincopy
- #include <stdio.h>
- #define ADD_TO_SUM(SUM_NUMBER,VALUE) \
- sum##SUM_NUMBER += VALUE
- int main(void){
- int sum5 = 1;
- ADD_TO_SUM(5,1);
- printf("sum_5 is:%d\n",sum5);
- return 0;
- }
输出结果是:
sum_5 is:2
##将左右的符号连接成一个符号,使之成为一个变量,如果新组成的变量必须要存在于代码中。
时间: 2024-11-12 10:30:33