为什么#定义在C布尔宏而不是简单地作为1 TRUE(1 == 1)? [英] Why #define TRUE (1==1) in a C boolean macro instead of simply as 1?

查看:165
本文介绍了为什么#定义在C布尔宏而不是简单地作为1 TRUE(1 == 1)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我见过的定义,用C

#define TRUE (1==1)
#define FALSE (!TRUE)

这个必要吗?有什么比简单地定义为TRUE 1效益,假以0?

Is this necessary? What's the benefit over simply defining TRUE as 1, and FALSE as 0?

推荐答案

此方法将使用实际布尔类型(和解决真实)如果编译器支持它。 (具体而言,C ++)

This approach will use the actual boolean type (and resolve to true and false) if the compiler supports it. (specifically, C++)

不过,这将是更好地检查C ++是否在使用(通过 __ CPLUSPLUS 宏)和实际使用真正

However, it would be better to check whether C++ is in use (via the __cplusplus macro) and actually use true and false.

在C编译器,这相当于 0 1 。结果
(注意去掉括号将打破,由于操作的顺序)

In a C compiler, this is equivalent to 0 and 1.
(note that removing the parentheses will break that due to order of operations)

这篇关于为什么#定义在C布尔宏而不是简单地作为1 TRUE(1 == 1)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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