c中define的奇怪错误
我知道在编译成实际值之前,define被替换了。那么,为什么这里的第一个代码编译时没有错误,而第二个代码编译时没有错误呢 第一c中define的奇怪错误,c,compiler-errors,printf,c-preprocessor,C,Compiler Errors,Printf,C Preprocessor,我知道在编译成实际值之前,define被替换了。那么,为什么这里的第一个代码编译时没有错误,而第二个代码编译时没有错误呢 第一 #include <stdio.h> #include <stdlib.h> int main() { printf("bc"); return 0; } 第二个不工作 #include <stdio.h> #include <stdlib.h> #define Str "bc"; int main() {
#include <stdio.h>
#include <stdlib.h>
int main()
{
printf("bc");
return 0;
}
第二个不工作
#include <stdio.h>
#include <stdlib.h>
#define Str "bc";
int main()
{
printf(Str);
return 0;
}
error: expected ')' before ';' token
谢谢你的回答,很抱歉我的英语很差。事实上,第二个有效,第一个无效。问题是分号:
#define Str "bc";
^
第一个代码无法编译,因为在定义后需要删除分号。第二个代码正常工作。因为Str宏的计算结果为bc;-分号包括在内。因此,您的宏将扩展为:
printf("bc";);
不需要在define后面加分号。它们以换行结束,而不是像C语句那样以分号结束。我知道,这令人困惑;C预处理器是一种奇怪的怪兽,在人们了解它之前就被发明了。使用
#define Str "bc"
使用替换后的“定义”,它将如下所示:
printf("bc";);
第一个问题是Str被bc;替换 换成
#define Str "bc"
你需要删除;定义str,因为您将获得printfbc 第一个不起作用,因为这些行:
#define Str "bc";
printf(Str);
展开到此行:
printf("bc";);
你想要:
#define Str "bc"
你确定第一个正在编译吗?该错误看起来像是由于以下原因造成的:;谢谢,我错了,我替换了两个代码块谢谢大家,我忘记了分号。