使用枚举作为条件,if 条件将返回什么,真或假?

Using enum as a condition, what will the if condition return, True or False?

本文关键字:条件 什么 返回 if 枚举      更新时间:2023-10-16
enum segment
{
OFF,
ON
};
int main()
{
segment indicator;
int temp_prev = 37;
int temp_curr = 39;
indicator = OFF;
if ((temp_curr > temp_prev and temp_curr > 39) or !indicator)
{}

这是了解enum用途和属性的基本程序的一部分。

让我困惑的是,!enum会返回什么,if条件会返回什么?

(假设 C( 枚举存储为 int。在您的示例中,OFF 将默认为零,这在条件中被视为 false,任何非零值都将被视为 true。

让我

感到困惑的是!enum返回的内容

一元!是逻辑 NOT 运算符。枚举值隐式转换为基础整数类型。如果该整数为 0,则结果为 true,否则为 false。OFF的基础值为 0,因此在这种情况下结果为 true,整个条件也是如此。