为什么编译不会失败? [英] Why Compilation Does Not Fail?

查看:41
本文介绍了为什么编译不会失败?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

按预期,C/C ++编译的确会失败并显示警告:指针与整数之间的比较".对于以下程序:

As expected, C/C++ compilation does fail with "warning: comparison between pointer and integer" for the program below:

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

但是,当 true 文字更改为 false 时,编译不会失败.为什么?

But, the compilation does not fail when the true literal is changed to false. Why?

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

推荐答案

在C中,宏 false 定义为:

#define false   0

因此,您正在将指针与0(这是一个有效的空指针常量)进行比较.

So you're comparing a pointer against 0, which is a valid null pointer constant.

这篇关于为什么编译不会失败?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆