MiddleCodeOccasionalNot answered yet
What does this signed/unsigned comparison print?
Predict what this comparison prints.
int i = -1;
unsigned u = 1;
std::cout << (i < u);
Determine the output and explain.
0 (false). In a mixed comparison the int is converted to unsigned, so -1 becomes a huge value (UINT_MAX), which is not less than 1. This usual-arithmetic-conversion trap also breaks for (size_t i = v.size() - 1; i >= 0; --i).
- ✗Reasoning about the operands as mathematical integers, ignoring the conversion
- ✗Believing the unsigned operand is promoted to signed
- ✗Writing
size_tcountdown loops that never terminate
- →What are the usual arithmetic conversions for mixed signed/unsigned operands?
- →Why does
for (size_t i = n - 1; i >= 0; --i)loop forever?