为什么编译不会失败?

sqr*_*163 -2 c c++ compiler-errors compiler-warnings

正如预期的那样,对于以下程序,C/C++ 编译确实失败并显示“警告:指针和整数之间的比较”:

#include <stdbool.h>
int main(void) { return (int*)42 == true; }
Run Code Online (Sandbox Code Playgroud)

但是,当true文字更改为false. 为什么?

  • 确认:clang-1100.0.33.12,gcc 7.5.0
  • 无法确认:g++ 7.5.0

dbu*_*ush 6

在 C 中,宏false定义为:

#define false   0
Run Code Online (Sandbox Code Playgroud)

因此,您将指针与 0 进行比较,这是一个有效的空指针常量。

  • 请明确这个答案是针对C的,至于C++则不正确。 (2认同)