Practice mode C++

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.

Report a problem

Answer checked How we write and check questions

Tip: press A, B, C or D to answer, and N for the next question.

Advertisement

Have a question about this answer?

Your email address will not be published. Required fields are marked *

Links and website addresses are not allowed in comments.