Im practicing interview questions but am having a hard time with this basic question:
How many times will this loop execute?
unsigned char half_limit = 150;
for (unsigned char i = 0; i < 2 * half_limit; ++i)
{
std::cout << i;
}
My thought is that since an unsigned int only reaches 255 it will execute forever since when I increment an unsigned char when it is at 255 it will revert back to 0? However this thinking is wrong and what is even more strange is that this is the output cout is giving me:
!"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~���������������������������������������������������������������������������������������������������������������������������������
And when I try to limit the look with something like the following:
if (i <= 255)
std::cout << i;
else
break;
The loop is still infinite.
My questions is what is the expected result of the code and why is this the case?
iisunsigned char.