#include <iostream> int main() { int a = 5, b = 10, c = 15; std::cout << (a < b < c) << std::endl; return 0; }
In C++, the expression a < b < c is parsed left-to-right as (a < b) < c due to left associativity of relational operators.
a < b is true (value 1 when contextually converted to int).
Then 1 < c (1 < 15) is true (1).
Relational operators return bool; std::cout prints 1 for true.
#include <iostream> int main() { int x = 7; double y = 7.0; std::cout << (x == y) << std::endl; return 0; }
The integer x is converted to double for comparison. Since 7 and 7.0 are equal, the expression x == y is true, so output is 1.
#include <iostream> int main() { int arr[3] = {1, 2, 3}; int* p1 = &arr[0]; int* p2 = &arr[2]; std::cout << (p1 < p2) << std::endl; std::cout << (p2 < p1) << std::endl; return 0; }
Pointer p1 points to arr[0], p2 points to arr[2]. Since p1 is before p2 in memory, p1 < p2 is true (1), and p2 < p1 is false (0).
#include <iostream> class Box { public: int volume; Box(int v) : volume(v) {} bool operator<(const Box& b) const { return volume < b.volume; } }; int main() { Box b1(10), b2(20); std::cout << (b1 < b2) << std::endl; std::cout << (b2 < b1) << std::endl; return 0; }
The operator< compares volumes. Since 10 < 20 is true (1), and 20 < 10 is false (0), the output is 1 then 0.
According to IEEE floating-point rules, any comparison involving NaN returns false, except inequality (!=) which returns true. But relational operators (<, >, <=, >=) always return false when NaN is involved.