c++ - C中的&&运算符是什么

标签 c++ c operators compiler-optimization gcc-warning

#include <stdio.h>

volatile int i;

int main()
{
    int c;

    for (i = 0; i < 3; i++) 
    {
         c = i &&& i;
         printf("%d\n", c);
    }

    return 0;
}

上面使用gcc编译的程序的输出是

0
1
1

使用 -Wall-Waddress 选项,gcc 会发出警告:

warning: the address of ‘i’ will always evaluate as ‘true’ [-Waddress]

c 在上述程序中是如何被评估的?

最佳答案

它是 c = i && (&i);,第二部分是多余的,因为 &i 永远不会评估为 false

对于用户定义的类型,您实际上可以重载一元 operator &,它可能会有所不同,但这仍然是一个非常糟糕的主意

如果您打开警告,您会得到如下信息:

warning: the address of ‘i’ will always evaluate as ‘true’

关于c++ - C中的&&运算符是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13946938/

相关文章:

c++ - 在 C/C++ 中使用 ~ 的 1 补码

c++ - 互斥实现和信号

java - 什么是 VM,为什么动态语言需要 VM?

c - HackerRank 上的对角线差异

java - Actionscript 中 "If greater than, then equal to"的简写?

c++ - 使用 AVL 树的缺点是什么?

C++ - 如何使用 Curlpp 或 libcurl 发送 HTTP post 请求

php - 如何调试 PHP 的内部 C 代码?

c++ - '&' 在 C++ 声明中做了什么?

c++ - 如何通过对 2 位或更多位数字使用 XOR 运算符来解决此 C++ 问题