printf(“LIST.H”) 其中 LIST.H 是宏
#include <stdio.h>
#include<stdlib.h>
#define LIST.H onus;
int main ()
{
char *p,*s;
printf(" LIST.H ");
}
我希望 LIST.H 打印 onus
作为输出。 但这并没有发生。 编译后我收到警告
temp.c:3:13: warning: missing whitespace after the macro name
,输出是 LIST.H 而不是责任。 我怎样才能得到上面宏打印出的想要的东西?
更新
我想要输出 作为 onus
,字符串前后各有一个空格。
#include <stdio.h>
#include<stdlib.h>
#define LIST.H onus;
int main ()
{
char *p,*s;
printf(" LIST.H ");
}
I expect LIST.H to print onus
as out put.
But this does not happen.
upon compiling I get a warning
temp.c:3:13: warning: missing whitespace after the macro name
and the output is LIST.H not onus.
How can I get desired thing printed by the above macro?
UPDATE
I want to have the output
as onus
with one space before and after the string.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
宏名称中不能包含
.
。这就是为什么你会收到警告:警告:宏名称后面缺少空格
,在LIST
之后它需要一个空格,但它得到一个.
。此外,当宏名称位于字符串内(在
"string"
之间)时,它不会被宏定义替换。您可以这样做:
预处理器将转换为:
如果您这样做:
预处理器会将其转换为:
它将无法编译。
Macros names cannot have
.
inside them. That's why you get the warning:warning: missing whitespace after the macro name
, afterLIST
it expects a space, but it gets a.
instead.Also, when a macro name is inside a string(between
"string"
) it is not replaced by the macro definition.You could do this instead:
which the preprocessor will transform to:
If you do:
the preprocessor will transform it to:
which won't compile.
首先,您不能在宏名称中使用
.
。其次,您应该“期望”它打印
ouns;
,因为您在宏定义中包含了;
。第三,为了实现这一点,您可以使用“字符串化”宏运算符
#
和一些辅助宏,或者更好
Firstly, you can't use
.
in macro names.Secondly, you should "expect" it to print
ouns;
, since you included a;
into your macro definition.Thirdly, in order to achieve that you can use "stringization" macro-operator
#
with some helper macrosor, better
字符串中的宏未解析,您需要宏解析层才能做到这一点:
我上次检查的宏定义中也不能有点,这就是您的错误所在,因此最好使用
LIST_H
...macros in strings aren't resolved, you need to layers of macro resolution to do that:
you also cannot have dots in macro defines last I checked, which would be what your error is about, so rather use
LIST_H
...