返回信息流#define STR abc
char arr[10] = "STR";
为什么arr数组中是STR而不是abc,define不是简单的字符替换吗?
这是一条镜像帖。来源:北邮人论坛 / cpp / #16581同步于 2008/11/24
该镜像源已超过 30 天没有更新,可能在源站已被删除。
CPP机器人发帖
#define 和 字符串
white127
2008/11/24镜像同步6 回复
订阅后,新回复会通过你的通知中心匿名送达。
6 条回复
#define A aaa
#define B bbbA //想要把A中的字符串用于define B bbbA中,即B是代表bbbaaa,该怎么定义
#define A "aaa"
#define B "bbb"A
我对macro这个东西不大熟悉……不过这样可以成功printf("%s\n",B);
【 在 white127 的大作中提到: 】
: #define A aaa
: #define B bbbA //想要把A中的字符串用于define B bbbA中,即B是代表bbbaaa,该怎么定义
我借楼问一下为什么下面这样写就不行呢……
[quote]
#include <stdio.h>
#define A "aaa"
#define B(x) "bbb"##x
int main()
{
printf("%s\n",B(A));
return 0;
}
[/quote]
用gcc test.c就报错了:
test.c:8:1: error: pasting ""bbb"" and "A" does not give a valid preprocessing token
然后试着分解执行:
gcc -E test.c>new.c #这里发现预处理后的结果和#define B "bbb"A 然后printf("%s\n",B);是完全一样的
gcc -S new.c
gcc -c new.s
gcc new.o
./a.out
居然成功执行了,不同的只是在预处理的时候在标准错误输出了上面的错误提示。
【 在 Vampire (吸血的鬼) 的大作中提到: 】
: #define A "aaa"
: #define B "bbb"A
: 我对macro这个东西不大熟悉……不过这样可以成功printf("%s\n",B);
: ...................
【 在 Xer 的大作中提到: 】
: 我借楼问一下为什么下面这样写就不行呢……
: [quote]
: #include <stdio.h>
: ...................
可能和编译器有关?
我这里gcc报warning
test.c:7:26: warning: pasting ""bbb"" and "A" does not give a valid preprocessing token
./a.out