# 的用法
#
用来将宏参数转换为字符串,也就是在宏参数的开头和末尾添加引号。例如有如下宏定义:
#include <stdio.h>
#define STR(s) #s
int main() {
printf("%s\n", STR(hello));
printf("%s\n", STR("how are you"));
return 0;
}
功能就是让输入的参数加上双引号变为字符串,
可以发现,即使给宏参数“传递”的数据中包含引号,使用
#
仍然会在两头添加新的引号,而原来的引号会被转义。
##的用法
##
称为连接符,用来将宏参数或其他的串连接起来。例如有如下的宏定义:
#define CON1(a, b) a##e##b
#define CON2(a, b) a##b##00
那么:
printf("%f\n", CON1(8.5, 2));
printf("%d\n", CON2(12, 34))
将被展开为
printf("%f\n", 8.5e2);
printf("%d\n", 123400);
将上面的例子补充完整:
#include <stdio.h>
#define CON1(a, b) a##e##b
#define CON2(a, b) a##b##00
int main() {
printf("%f\n", CON1(8.5, 2));
printf("%d\n", CON2(12, 34));
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#define hehe(x,y) x##y
int main()
{
char string[]="hello world!";
printf("%s\n",hehe(str,ing));
system("pause");
return 0;
}