expected declaration specifiers or解决方法
expected declaration specifiers or
C语言通过#ifdef预处理器定义宏的控制结构的时候,C语言的编译器弹出错误提示:expected declaration specifiers or '...' before string constant。比如下方的这个实例代码。这是为什么呢?
#include <stdio.h>
#ifdef ABC
printf("abc");
#else
printf("XYZ")
#endif
int main() {
return 0;
}
原因
C语言编译时出现这种提示,主要原因在于printf()函数是C语言中的一个标准库函数,是在C语言程序运行时执行的,而不是在C语言的预处理器中,因为预处理器是在C语言程序编译之前执行,因此无法在预处理器中直接调用printf()函数。
解决方法
可以将printf()函数改成#define定义一个需要宏或宏函数,因为在宏函数中则可以使用printf()“函数”,因为这只是一个替换文本。
实例代码
#include <stdio.h>
#ifdef ABC
printf("abc");
#else
#define DEF() printf("HELLO")
#endif
int main() {
DEF();
return 0;
}
免责声明:内容仅供参考,不保证正确性。