【问题标题】:syntax error when using #ifdef in c在c中使用#ifdef时出现语法错误
【发布时间】:2014-11-22 04:22:17
【问题描述】:

我正在尝试使用和不使用“-D DEBUG”gcc 标志运行以下代码:

#include <stdio.h>

#ifdef DEBUG
    printf("Defined");
#else
    printf("Not defined");
#endif

int main() 
{
}

我得到的错误是“debugtest.c:6:9: error: expected declaration specifiers or ‘...’ before string constant”

【问题讨论】:

  • 你不能有函数之外的语句。
  • 如果您想查看运行预处理器后代码的样子,请运行gcc -E debugtest.c。这将从#include &lt;stdio.h&gt; 打印一堆东西到屏幕上,然后你会看到在main 之前有一个孤独的printf

标签: c


【解决方案1】:

您对printf 的调用必须函数中:

#include <stdio.h>

int main() 
{
#ifdef DEBUG
    printf("Defined");
#else
    printf("Not defined");
#endif
    return 0;    
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-01-27
    • 2013-10-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多