c - 如何修复警告 : '&&' within '||'

标签 c gcc

<分区>

这是 GCC 的问题吗?我试过在 Windows 上运行,但没有发出此警告。有没有办法(除了插入括号)解决这个问题?

#include <stdio.h>

int x, y;

int main (void)
{
x = 3;
y = 7;

if ((y < 2) || (y > 5) && (x < 12)) {
    printf ("Yes\n");
} else
    printf ("Nope\n");
return 0;
}

结果:

/Users/filename:18:28: warning: '&&' within '||' [-Wlogical-op-parentheses]
    if ((y < 2) || (y > 5) && (x < 12)) {
                ~~ ~~~~~~~~^~~~~~~~~~~
/Users/filename:18:28: note: place parentheses around the '&&' expression to
      silence this warning
    if ((y < 2) || (y > 5) && (x < 12)) {
                           ^
                   (                  )

最佳答案

你的答案是here :

The natural tendency is to read it left to right, and it's easy to forget the operator precedence. That said, it's just a warning, and if you know what you're doing and your own style allows it, feel free to suppress it.

并抑制它:

For those who would like to suppress it, append: -Wno-logical-op-parentheses to your build-system’s CFLAGS

关于c - 如何修复警告 : '&&' within '||' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33716845/

相关文章:

c++ - 为什么 gcc 和 clang 会为 std::find 生成这么多代码?

c - C 预处理器如何处理循环依赖?

c - 我如何知道在 Linux 上使用哪个串口?

c - 在C中扫描多个单词

c - 如何读取整数数组

docker - Alpine 上哪个库包含 cdefs.h?

c - 我怎样才能让 GCC 用 SSE 指令向量化这个简单的复制循环?

windows10上用VIM编译C程序文件

c - 如何使用宏来调用函数?

c - pthread_create 的第三个参数需要哪种类型