比较运算符的行为是什么?
What's the behaviour of the Comparison operator?
可能的重复:
双重比较
int x=-1;
if(0<=x<=9)
std::cout<< "Without Logical operator";
if (0<=x && x<=9)
std::cout<< "With Logical operator";
我知道第二个if
,它运行良好。在第一个if
条件下发生了什么。它进入第一个if
,而x
是-1
以及为什么编译器在使用(0<=x<=9)
时没有给出error
在C中,布尔值只是纯整数。在布尔上下文中,0
为false,所有其他值均为true。在这种情况下,
(0 <= x <= 9) ==
((0 <= x) <= 9) == // the (0 <= x) evaluates to 0, which is false in boolean context
(0 <= 9) ==
1 (true)
相关文章:
- 重载运算符的范围是什么?它是否会影响作为类成员的集合的插入函数?
- "operator()"在重载运算符方法中是什么意思,在priority_queue(STL)中用作C++中的比较器?
- 是什么让一些命名函数/运算符与众不同?
- C++,()运算符重载,它的工作是什么
- std::set<Key,Compare,Allocator>::find() 函数使用"<"运算符而不是"=="运算符背后的直觉是什么?
- 使输出流式处理运算符适用于 boost::variant<std::vector<int>、int、double 的正确方法是什么>
- c++运算符重载-我实际返回的操作数类型是什么
- 这个typedef和转换运算符语法是什么意思
- 运算符和返回类型是什么意思?
- 双标具有下标运算符的原因是什么?
- 统一初始化是隐式发生的,即使 int 强制转换运算符是使用 explicit 关键字声明的.原因是什么?
- _bstr_t::运算符=是什么意思?
- Python的"in"运算符的C++等价物是什么?
- 将强制转换运算符重载到 std::map 的正确语法是什么
- typeid 运算符忽略 cv 限定符背后的理由是什么?
- 初始化中的模板转换运算符类型推导规则是什么?
- 运算符在C++" "是什么?
- "<<"和">>"运算符的名称是什么?
- 此代码运算符超载中的错误是什么?
- 将多个值与同一变量进行比较时'!='运算符最简洁的方法是什么?