Reputation: 101
I'm having problem with this for loop: The compiler says: "error: comparison is always true due to limited range of data type [-Werror=type-limits]" referring to the "for(...)" part. I don't understand why, since the comparison isn't always true.
for(unsigned char i = 7; i >= 0; i--)
{
if(dec % 2 == 0) binary[i] = '0';
else binary[i] = '1';
if(dec/2 <= 0) return binary; else dec /= 2;
}
Upvotes: 0
Views: 472
Reputation: 11430
When i
is 0, i >= 0
will be true, and you'll apply i--
.
Then, what value do you expect i
to take ? It cannot take the -1
value, because it is unsigned, so i
will become 255.
But 255 is >=0
so the loop will continue. Forever.
Instead, consider using:
for(int i = 7; i >= 0; i--)
Upvotes: 4