Why does the following c code end up in an infinite loop?
for(unsigned char i = 0; i <= 0xff; i++){}
It is the same result with:
for(unsigned char i = 0; i <= 0xff; ++i){}
In which way do I have to modify the code, to get it working as expected (without using int
or unsigned int
datatype)?
If you really need to use unsigned char
then you can use
unsigned char i = 0;
do {
// ...
} while(++i);
When an arithmetic operation on an unsigned
integer breaks its limits, the behaviour is well defined. So this solution will process 256 values (for 8-bit unsigned char
).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With