Assuming standard headers and using namespace std, what does this C++ code print? unsigned int u = 0; u–; cout << (u > 0);
Correct answer A. 1
Explanation
Unsigned integer arithmetic in C++ is performed modulo 2^N, so decrementing an unsigned 0 wraps round to the largest representable value (UINT_MAX), which is greater than 0. The comparison is true and bool prints as 1. Unlike signed overflow, unsigned wrap-around is fully defined behaviour, which is why loops counting down with unsigned counters can run forever.
Answer checked How we write and check questions
Discussion