gpt4 book ai didi

c - 使用else宏 “expected parameter declarator”时发生C错误

转载 作者:行者123 更新时间:2023-12-03 08:41:32 25 4
gpt4 key购买 nike

我编写了以下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'

最佳答案

使用条件编译时,预处理器会在编译步骤之前将代码添加到程序中。
因此,如果符号_WIN32存在,那么您实际上是在说

printf("Running on Windows");

void test(int x);

int main() {
return 0;
}

这在语法上是不正确的,因为您在所有函数之外都具有可执行代码(对printf的调用)。

如果在添加“#else”之前没有问题,那是因为符号_WIN32不存在,并且预处理程序未在代码中添加printf语句。

关于c - 使用else宏 “expected parameter declarator”时发生C错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61156241/

25 4 0