宏展开基础一问
czj@sun:/tmp$ cat test.c#include <stdio.h>
#define SAY(s) #s
#define say(s) SAY(s)
#define HELLO "hello\nworld"
int main()
{
printf("%s\n\n", HELLO);
printf("%s\n", say(HELLO));
return 0;
}
czj@sun:/tmp$ ./a.out
hello
world
"hello\nworld" #-------反推say(HELLO)展开后的结果为"\"hello\\nworld\"",这是怎么做到的?反推say(HELLO)展开后的结果为"\"hello\\nworld\"",为什么经过
#define SAY(s) #s
#define say(s) SAY(s)
这样的两步能做到这一点
本帖最后由 MMMIX 于 2016-12-10 20:49 编辑
回复 1# cao627
say(HELLO) 展开之后是 SAY("hello\nworld"),而在 C 的宏语法中,#s 的作用就是把参数 s 转换成其对应的字符串形式。 回复 2# MMMIX
谢谢指点!
页:
[1]