c - GCC 可以编译,但 clang 不会

标签 c gcc clang

所以在我的期末考试中,有一个练习询问程序将打印什么。

这是代码:

#include <stdio.h>

int main (){
    int x=5, y=4;

    if (x>y);
      printf("A");

    if(x=4)
      printf("%d",x+y);

     return 0;
}

当我尝试在 Debian 机器上使用 gcc -ansi -pedantic -Werror 编译它时,它编译得很好并输出“A8”。

但是,当我尝试使用 clang -ansi -pedantic -Werror 编译它时,我收到有关 if (x=4) 表达式缺失 的错误= 且缺少 if(x>y); 上的语句。

为什么会发生这种情况,哪个答案可以标记为正确?

最佳答案

编译器可能会发出不同的警告。例如,clang 会对空语句发出警告,而 gcc 则不会。

因此,如果您设置 -Werror 它将使用 gcc 进行编译,但不会使用 clang 进行编译。

顺便说一句,是什么阻止您阅读编译器消息。它们是不言自明的。它甚至展示了如何纠正它。

#1 with x86-64 clang 9.0.0
<source>:6:9: error: if statement has empty body [-Werror,-Wempty-body]

if (x>y);

        ^

<source>:6:9: note: put the semicolon on a separate line to silence this warning

<source>:8:5: error: using the result of an assignment as a condition without parentheses [-Werror,-Wparentheses]

if(x=4)

   ~^~

<source>:8:5: note: place parentheses around the assignment to silence this warning

if(x=4)

    ^

   (  )

<source>:8:5: note: use '==' to turn this assignment into an equality comparison

if(x=4)

    ^

    ==

<source>:12:2: error: no newline at end of file [-Werror,-Wnewline-eof]

}

 ^

3 errors generated.

Compiler returned: 1

关于c - GCC 可以编译,但 clang 不会,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59685730/

相关文章:

macos - 在安装了 Developer Tools 的情况下为 OSX 获取 GCC

objective-c - 从框架伞头中排除内部头

java - C-DLL 中的回调函数转换为 java JNA

C 指针段错误

c - 如果类型是指针,则函数返回值的范围

c++ - 将 OpenMP 与 clang 一起使用

c++ - 为什么我收到 clang 警告 : no previous prototype for function 'diff'

c++ - 我如何使用 gcc/g++ 预处理器删除不活动的 #if 指令?

c - 错误 : 'uint16_t' undeclared?

c++ - 确定方法或成员是否可以 protected 或私有(private)