简体   繁体   English

C 错误与使用 else 宏“预期参数声明符”

[英]C error with using else macro “expected parameter declarator”

I wrote the following C code (according to the C99 standard) and it ran with no problems:我编写了以下 C 代码(根据 C99 标准)并且运行没有问题:

#include <stdio.h>

#ifdef _WIN32
printf("Running on Windows");
#endif

void test(int x);

int main() {
    return 0;
}

but adding else caused so much errors (around 12) what's the problem with the new code:但是添加else会导致很多错误(大约 12 个)新代码有什么问题:

#ifdef _WIN32
printf("Running on Windows");
#else
printf("Running on Windows");
#endif

Some of the errors:一些错误:

error: expected parameter declarator
expected ')'
warning: type specifier missing, defaults to 'int' [-Wimplicit-int]
error: conflicting types for 'printf'

When you use conditional compilation the preprocessor adds code to your program before the compile step.当您使用条件编译时,预处理器会在编译步骤之前将代码添加到您的程序中。 So, if the symbol _WIN32 exists, then you are effectively saying所以,如果符号 _WIN32 存在,那么你实际上是在说

printf("Running on Windows");

void test(int x);

int main() {
    return 0;
}

and that is syntactically incorrect because you have executable code (a call to printf) outside all functions.这在语法上是不正确的,因为您在所有函数之外都有可执行代码(对 printf 的调用)。

If you didn't have a problem before you added the '#else' then that was because the symbol _WIN32 doesn't exist and the preprocessor wasn't adding the printf statement to your code.如果您在添加“#else”之前没有遇到问题,那是因为符号 _WIN32 不存在并且预处理器没有将 printf 语句添加到您的代码中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM