【发布时间】:2020-07-24 03:27:17
【问题描述】:
我编写了以下 C 代码(根据 C99 标准)并且运行没有问题:
#include <stdio.h>
#ifdef _WIN32
printf("Running on Windows");
#endif
void test(int x);
int main() {
return 0;
}
但是添加 else 导致了很多错误(大约 12 个)新代码有什么问题:
#ifdef _WIN32
printf("Running on Windows");
#else
printf("Running on Windows");
#endif
一些错误:
error: expected parameter declarator
expected ')'
warning: type specifier missing, defaults to 'int' [-Wimplicit-int]
error: conflicting types for 'printf'
【问题讨论】:
-
您不能在文件作用域中调用函数。
printf("...");需要在函数内部。 -
您需要发布使用宏的上下文。正如@PSkocik 所说,这个宏需要在一个函数中,这样对 printf 的调用才能正确编译。
-
我不明白为什么第一个代码可以工作?
-
@clark_smith 可能是因为
_WIN32没有定义,所以预处理器没有插入printf调用。 -
如果您使用像
gcc/clang/tcc这样的编译器,请使用-E运行它以查看预处理后的文本输出。
标签: c if-statement error-handling macros c99